Exception Handling in Java

In the previous tutorial, we have discussed what is an Exception and the different types of exceptions in Java. In this tutorial, we are going to see the Exception Handling (i.e.) how to handle the Exception in Java.

Exception Handling Flow Diagram :

Exception-Flow

To handle exceptions, Java gave us the following keywords:

  1. try
  2. catch
  3. finally
  4. throw
  5. throws

1. try block

In Java when we are not certain about the outcome of a statement, we can put those statements inside this try block.  A try block can be used in a method or another try block.

Syntax:

public void someMethod(){
    try{
        // Statements
    }
}

2. catch block

In Java, the catch block is used to handle the exception which is caught from the try block. While executing the statements in a try block, if the system encounters an exception then it skips out rest of the statements and transfers the control to the corresponding catch block. Hence the catch block is executed, if and only if an exception is raised. Otherwise, it will not be executed.

Note: The catch block will always follow after the try block. Once the control comes out from the try block, there is no way of taking it back in to try block.

Syntax:

catch(Exception e){
    // Handling an Exception
    }

3. finally block

A finally block is a block of statements, that are executed irrespective of whether we get an exception or not. Hence the statements in the finally block are executed compulsorily. Typically, finally blocks are used to nullify the object references and closing the I/O streams.

Syntax :

public void someMethod(){
    try{
        // Statements
    }
    catch(Exception e){
        // Handling an Exception
    }
    finally{
        // Statements
    }
}

4. throw

The throw keyword is used to raise the exception explicitly and thus making explicit exception propagation. Only Throwable or subclasses of Throwable can be thrown using this throw keyword.

Syntax:

public void someMethod(){
    try{
        // Statements
    }
    catch(Exception e){
        // Handling an Exception
        throw new ArithmeticException("I am Throwing.");
    }
    finally{
        // Statements
    }
}

5. throws

In Java, throws keyword is applied to state an exception. If a method is capable of generating Checked Exception that if we don’t know how to handle, then we can throw that checked exception by using this throws keyword.

Syntax:

returnType methodName() throws ExceptionClassName{  
    //code for the method  
}  

Example for Exception Handling

Exception Handling.java
public class ThrowsAndThrowDemo {
    public static void main(String[] args) throws InterruptedException {
        try {
               System.out.println("First Statement");
               if (true) {
                   Thread.sleep(1000);
                   throw new java.io.IOException();
               }
               System.out.println("Another Statement");
        } 
        catch (java.io.IOException e) {
            e.printStackTrace();
        } 
        finally {
            System.out.println("In Finally Block");
        }
    }
}

Output:

Exception Handling.java
First Statement 
java.io.IOException at ThrowsAndThrowDemo.main(ThrowsAndThrowDemo.java:13) 
In Finally Block

References:

Happy Learning 🙂