Exception handling keywords

try block
The try block in Java contains the code that might thrown an error. This block must be followed by either catch or finally or both.
Let’s see an example

catch block
The catch block is used to handle the exception. This block must be used after the try block only.Single try block can have multiple catch blocks so that the control of execution is passed to corresponding catch block.
Note: At a single time only one catch block will be executed. Catch blocks should be in such a way that the generic exception must be at the end.
Lets see an example

class ExceptionTest {

  public static void main(String[] args) {
    int result;
    try {
      result = 10 / 0;
    } catch (ArithmeticException e) {
      System.out.println("Arithmetic exception occurs here");
    } catch (ArrayIndexOutOfBoundsException e) {
      System.out.println("Array Index Out Of Bound Exception occurs here");
    } catch (Exception e) {
      System.out.println("Common exception here");
    }
    System.out.println("Remaining code here...");
  }
}

Output:

Arithmetic exception occurs here
Remaining code here…

nested try-catch block
The try-catch with in another try block is known as nested try-catch block. A try, catch and finally block can contain another set of try-catch block.

Lets see an example

class ExceptionTest {

  public static void main(String[] args) {
    try {
      System.out.println("Outer try block");
      try {
        System.out.println("Inner try block");
        int result = 10 / 0;
      } catch (ArrayIndexOutOfBoundsException e) {
        System.out.println("Array Index Out Of Bound Exception");
      } finally {
        System.out.println("Inner Final");
      }
    } catch (ArithmeticException e) {
      System.out.println("Arithmetic Exception");
    } finally {
      System.out.println("Outer Finally");
    }
  }
}

Output:

Outer try block
Inner try block
Inner Final
Arithmetic Exception
Outer Finally Block

finally block
Finally block is a block that is used to execute essential code like closing database connection, objects and streams etc.It will be executed regardless the exception is handled or not.Finally block follows try or catch block.For each try block it might be 0 or multiple catch blocks but there will be single finally block.

Note: It will not be executed in few situation like intentionally we are calling System.exit() method or due to some fatal error. We have also seen finally example in above section.

throw
The throw keyword is used to explicitly throw an exception. We can throw any of the exception like checked or unchecked exception. Throw keyword is mainly used to throw user defined or custom exceptions. The user defined exception must be of type Throwable or a subclasses of Throwable.

Note: We can not throw multiple exceptions.

Lets see an example:

class ExceptionTest {

  public static void main(String[] args) {
    try {
      throw new ArithmeticException("Arithmetic Exception Occurs Here");
    } catch (ArithmeticException e) {
      System.out.println(e.getMessage());
    }
  }
}

Output:

Arithmetic Exception Occurs Here

throws
The throws keyword is used to declare an exception and we can declare multiple exceptions through throws keyword. The throws keyword is used in method declaration. It declares that your method can throw an exception.
Note: When we use throw keyword, JVM take care the exception object while in throws it is the responsibility of caller method to handle exception.

Lets see an example:

import java.io.FileNotFoundException;
import java.io.FileReader;

class ExceptionTest {

  public static void displayFile() throws FileNotFoundException {
    FileReader file = new FileReader("E://file//test.docx");
  }

  public static void main(String[] args) {
    try {
      displayFile();
    } catch (FileNotFoundException e) {
      System.out.println(e);
    }
    System.out.println("Rest of the code");
  }
}

Output:

java.io.FileNotFoundException: E:\file\test.docx (The system cannot find the path specified)
Rest of the code