Any exception thrown from try block makes the control jump to catch block if it is caught by it
At least either catch or finally should be present with try
There can only be one finally but there can be multiple catch blocks
No matter whether exception is handled or not, or return statement is present or not, finally will ALWAYS execute
finally will not get executed if:
System.exit() is called in try or catch block
JVM is shutdown abruptly
return statement in finally block will always override try or catch’s return statement if present
The exception thrown from code in the try block, tries to find the first match in multiple catch blocks and executes it and then goes directly to finally block.
It is good practice is to go from specific exception to more generic one when specifying multiple catch blocks
It is compile time error if we write catch with general exception before catch with specific exception
Resource Classes that implements Closeable or AutoCloseable can be used in try-with-resources construct
Example
try (JpegReader jpegReader = new JpegReader(); PngReader pngReader = new PngReader()) { // your code}
In a try-with-resources statement, any catch or finally block is run after the resources declared have been closed
close() methods are called automatically in opposite order of their creation
pngReader.close() is called first, then jpegReader.close() is called
If the resource is evaluated to null in declaration, then close() method is not called internally, but you need to make sure it is not null when using inside try block
Following exceptions can be caught in the catch block:
If exception happen in resource declarations, then it is thrown
If exception happen in try-with-resource close methods, then it is thrown
If exception happen in try-with-resource close methods AND inside try block, then try block exception is thrown and try-with-resource’s exception is suppressed
class JpegReader implements AutoCloseable { public String read() { return "Anna.jpeg"; } @Override public void close() { System.out.println("JpegReader close is invoked"); }}class PngReader implements AutoCloseable { public String read() { return "Anna.png"; } @Override public void close() { System.out.println("PngReader close is invoked"); // this exception is suppressed // because there is already an exception in try block throw new RuntimeException("failed to close PngReader"); }}public class Test { public static void main(String[] args) { try (JpegReader jpegReader = new JpegReader(); PngReader pngReader = new PngReader()) { System.out.println(jpegReader.read()); System.out.println(pngReader.read()); throw new RuntimeException("try failed"); } catch (Exception e) { System.out.println(e.getMessage()); // print suppressed exception System.out.println(e.getSuppressed()[0].getMessage()); } } }
Output
Anna.jpegAnna.pngPngReader close is invokedJpegReader close is invokedtry failedfailed to close PngReader
try-finally vs try-with-resource
try-with-resource is better than try-finally construct
It is possible that jpegReader.close() throws some exception in finally block
This will cause resource leak of pngReader since it was not closed
JpegReader jpegReader = new JpegReader(); PngReader pngReader = new PngReader()try { return jpegReader.readLine();} finally { jpegReader.close(); // if it fails, pngReader.close() is not executed pngReader.close();}
Closable vs AutoClosable
Closeable extends AutoCloseable
Closeable
AutoCloseable
close() throws IOException
close() throws Exception
subclass must throw specialized exceptions of IOException