Exceptions

An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions. In Java there are two types of exceptions: checked and unchecked.

Checked exceptions must be handled inside try/catch block or method where a checked exception can occur must be marked with the throws keyword.

Unchecked exceptions may be unhandled. In this case, when an exception occurs, the application will crash with an exception message. All unchecked exceptions are child of the RuntimeException class. Therefore, they are sometimes called runtime exceptions.

Java has the following keywords related to exceptions:

  • try - defines a test block. There is a special syntax try-with-resource, which allows you to automatically close specified closable resources.
  • catch - defines a block of error handling
  • finally - defines a block that will be executed regardless of the test result
  • throw - throws the specified exception
  • throws - allows you to specify a list of exceptions that can be thrown in the method
public Integer methodGetEl(int[] array, int ind){
    try{
        return array[ind];
    }catch (Exception e){
        e.printStackTrace();
        return null; 
    }
}

public void method1(String filename) 
  throws FileNotFoundException {// mark method as throwable
    FileInputStream fis = new FileInputStream(filename);
    // ...
}

public void method2(String filename) {
    // fis will be closed after execution try block
    try(FileInputStream fis = new FileInputStream(filename)){
        // ...
    }catch (IOException ioe){
        ioe.printStackTrace();
    }
}

throw exception

The throw keyword allows to throw the specified exception.

You can throw or rethrow exception inside the catch block.

if(!check(arg)){
    throw new IllegalArgumentException("bla bla bla");
}
try{ // ... }catch (IOException ioe){ // ... do something and rethrow exception throw ioe; }

catch block

The catch block allows handle exceptions of the specified types.

The | is used for specifying several types of exceptions.

You may have several catch blocks. The catch block with the Exception class can be used as the default handling, because the Exception class is the base class for exceptions. Such block should be the last.

try{
    // ...
}catch(IllegalArgumentException|IllegalStateException
        |IOException e){
    // ...
}catch(Exception e){
    // ...
}finally{
    // ...
}

try-with-resource

There is a special syntax try-with-resource, which allows you to automatically close specified closable resources. Any catch or finally block is run after the resources declared have been closed. Closable means that resource implements the java.lang.AutoCloseable interface.

Prior Java 9 resources must be declared inside try-with-resource.

ZipFile zf = ZipFile(zipFileName); 

try (
    ZipFile zf1 = zf;
    BufferedWriter writer = Files.newBufferedWriter(outputFilePath, charset);
){
    // ...
}
ZipFile zf = ZipFile(zipFileName); BufferedWriter writer = Files.newBufferedWriter(outputFilePath, charset); // since Java 9 try(zf; writer){ }