Open In App

Java Try Catch Block

Last Updated : 02 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

try-catch block in Java is a mechanism to handle exceptions. This ensures that the application continues to run even if an error occurs. The code inside the try block is executed, and if any exception occurs, it is then caught by the catch block.

Example 1: Here, in the below example we handled the ArithmeticException using the simple try-catch block.

Java
import java.io.*;

class Geeks {
    public static void main(String[] args) {
        try {
          
            // This will throw an ArithmeticException
            int res = 10 / 0;
        }
        // Here we are Handling the exception
        catch (ArithmeticException e) {
            System.out.println("Exception caught: " + e);
        }

        // This line will executes weather an exception
        // occurs or not
        System.out.println("I will always execute");
    }
}

Output
Exception caught: java.lang.ArithmeticException: / by zero
I will always execute

Syntax of try Catch Block

try {

// Code that might throw an exception

} catch (ExceptionType e) {

// Code that handles the exception

}

1. try in Java

The try block contains a set of statements where an exception can occur.

try
{
// statement(s) that might cause exception
}

2. catch in Java

The catch block is used to handle the uncertain condition of a try block. A try block is always followed by a catch block, which handles the exception that occurs in the associated try block.

catch
{
// statement(s) that handle an exception
// examples, closing a connection, closing
// file, exiting the process after writing
// details to a log file.
}

Internal working of Try-Catch Block

  • Java Virtual Machine starts executing the code inside the try block.
  • If an exception occurs, the remaining code in the try block is skipped, and the JVM starts looking for the matching catch block.
  • If a matching catch block is found, the code in that block is executed.
  • After the catch block, control moves to the finally block (if present).
  • If no matching catch block is found the exception is passed to the JVM default exception handler.
  • The final block is executed after the try catch block. regardless of weather an exception occurs or not.

try{

// this will throw ArithmeticException

int ans = 10/0;

}catch(ArithmeticException e){

System.out.prinln(“caught ArithmeticException”);

}finally{

System.out.println(“I will always execute weather an Exception occur or not”);

}

Example 2: Here, we demonstrate the working try catch block with multiple catch statements.

Java
// Java Program to Demonstrate try catch block 
// with multiple catch statements
import java.util.*;

class Geeks {
    public static void main(String[] args) {
        try {

            // ArithmeticException
            int res = 10 / 0;

            // NullPointerException
            String s = null;
            System.out.println(s.length());
        }
        catch (ArithmeticException e) {
            System.out.println(
                "Caught ArithmeticException: " + e);
        }
        catch (NullPointerException e) {
            System.out.println(
                "Caught NullPointerException: " + e);
        }
    }
}

Output
Caught ArithmeticException: java.lang.ArithmeticException: / by zero

Example 3: Here, we demonstrate the working of nested try catch block.

Java
import java.util.*;

public class Geeks {
    public static void main(String[] args) {
        try {
          
            // Outer try block
            System.out.println("Outer try block started");

            try {
                // Inner try block 1
                int n = 10;
                int res = n / 0;  // This will throw ArithmeticException
            } catch (ArithmeticException e) {
                System.out.println
                  ("Caught ArithmeticException in inner try-catch: " + e);
            }

            try {
              
                // Inner try block 2
                String s = null;
                System.out.println(s.length());  // This will throw NullPointerException
            } catch (NullPointerException e) {
                System.out.println
                  ("Caught NullPointerException in inner try-catch: " + e);
            }

        } catch (Exception e) {
          
            // Outer catch block
            System.out.println
              ("Caught exception in outer try-catch: " + e);
        } finally {
          
            // Finally block
            System.out.println("Finally block executed");
        }
    }
}

Output
Outer try block started
Caught ArithmeticException in inner try-catch: java.lang.ArithmeticException: / by zero
Caught NullPointerException in inner try-catch: java.lang.NullPointerException
Finally b...


Next Article
Practice Tags :

Similar Reads