Java - Exception (Closeable)

Java Conceptuel Diagram


The Java mechanism for handling exceptions uses try and catch blocks. With Java, if a method throws an exception, there needs to be a mechanism to handle it (if it's not a Runtime (ie unchecked) Exception). Generally, a catch block catches the exception and specifies the course of action in the event of an exception, which could simply be to display the message.

In production code, it is not recommended to dump stack traces that are visible to the user.




Extend Exception


Extend RuntimeException

When the exception is Unchecked, you avoid the parsing error:

Unhandled exception

try (Closeable)

The try block may get Autcloseable object that will be closed at the end of the block


try (Connection sqlConnection = Driver.getConnection()) {
} // the connection will be close when the end of the block is reached

Other way to deal with closeable object apache/calcite/blob/master/core/src/main/java/org/apache/calcite/util/ (similar to

try (Closer closer = new Closer()) {


try {

} catch (ExceptionType name) {

} catch (NoSuchMethodException | IOException | IllegalAccessException | ClassNotFoundException e) {

} finally {



All methods use the throw statement to throw an exception.

throw someThrowableObject;
// Example with an exist Exception
throw new FileNotFoundException("Your Message");
throw new IllegalArgumentException("Your Message");

The Throwable class is the superclass of all errors and exceptions in the Java language. Only objects that are instances of this class (or one of its subclasses) are thrown by the Java Virtual Machine or can be thrown by the Java throw statement.


public class InvalidTypeException extends Exception {

  public InvalidTypeException(String message){



public void logException(SQLException ex) {
    while (ex != null) {
        ex = ex.getNextException();



To deal with this exception, you have first to check the reference of object with null

if (myObject != null) {
     System.out.println("NOT NULL");
} else {

of use optional in java 8. See Tired of Null Pointer Exceptions? Consider Using Java SE 8's Optional!

Discover More
Eclipse Java Compiler Compliance Level
Eclipse - How to resolve a java.lang.UnsupportedClassVersionError exception ?

How to deal with the java.lang.UnsupportedClassVersionError exception in Eclipse ? Verify the version of java Set the compliance level accordingly in the project property
Jdbc Class Architecture
JDBC - Oracle

Definition of the JDBC Oracle API or how to connect to a JDBC oracle and made database operations. A simple application includes the following classes: This class contains all...
Jconsole Memory Chart
JVM - Memory (The Heap)

The JVM memory consists of the following segments: Heap Memory, which is the storage for Java objects Non-Heap Memory, which is used by Java to store loaded classes and other meta-data JVM code...
Java Conceptuel Diagram
Java - (Static|Dynamic) Initialization blocks

Initialization block are used to initialize field with a complex logics such as: with a function that throw exceptions. check if a particular class is loaded only one execution with the static modifier...
Java Fileiomethods
Java - IO - Connection (Stream and Channel)

in Java. In order to perform I/O operations (for example reading or writing), you need to perform a connection. In Java, this connection are modelled through: a stream ( package) or a channel...
Java Conceptuel Diagram
Java - java.lang.NullPointerException (NPE)

The 1001 way on how I get a “Null Pointer Exception” In general, unless otherwise noted in the javadoc, methods and constructors will throw NullPointerException if passed a null argument. ...
Card Puncher Data Processing
Language - Assertion

In computer programming, an assertion is a predicate (for example a true–false statement) placed in a program to indicate that the developer thinks that the predicate is always true or false at that...

Share this page:
Follow us:
Task Runner