Try-catch block in Java

Try Catch in Java – Exception handling

Try block

A series of statements where an exception may occur are contained in the try block.

A try block is always followed by a catch block, which handles the exception that occurs in an associated try block.

Either a catch block, a finally block, or both must come after a try block.

Catch block

A catch block is where you handle the exceptions, this block must follow the try block.

Multiple catch blocks may be connected to a single attempt block. Different catch blocks allow you to capture different exceptions. The catch block that corresponds to the try block that handles that specific exception runs when an exception arises in the try block.

For example, if an arithmetic exception occurs in the try block then the statements enclosed in the catch block for arithmetic exception executes.

Syntax of try catch in java:

try
{
   //statements that may cause an exception
}
catch (exception(type) e(object))‏

    {
   //error handling code
}

Example of Try-catch Block

public class Exception Handling{
    public static void main(String[] args) {
        try {
            System.out.println("hello" + " " + 1/0);
            //System.out.println(1/0);
        }
        catch (ArithmeticException e)
        {
            System.out.println("world");
        }
    }
}

Output:

World

In the example, we are trying to divide a number by 0. Here, this code generates an exception. To handle the exception, we have put the code,  1 / 0 inside the Try block.

Currently, the remaining code within the Try block is bypassed in the event of an exception.

The Catch block catches the exception and statements inside the catch block are executed. The catch block is bypassed if none of the statements in the try block result in an exception.

Example of Multiple Catch Blocks:

After a try block, it is possible to include one or more catch blocks. Each catch block should include a unique exception handler. When there are multiple exceptions that require different actions to be taken, the Java multi-catch block should be used.

Points to remember

  • At a time only one exception occurs and at a time only one catch block is executed.
  •  All catch blocks must be ordered from most specific to most general, i.e. catch for ArithmeticException must come before catch for Exception.

Example of Multiple catch block:

public class MultipleCatchBlock{
    public static void main(String[] args) {
        try {
            int a[] = new int[10];
            a[10] = 50 / 0;
            
        } catch (ArithmeticException e) {
            System.out.println("Arithmetic Exception ");
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("ArrayIndexOutOfBounds Exception ");
        } catch (Exception e) {
            System.out.println("Parent Exception ");
        }
        System.out.println("Execution done");
    }

Output:

Arithmetic Exception

Execution Done

Example of Multiple Catch BlocKs

public class MultipleCatchBlock{
    public static void main(String[] args) {
        try {
            int a[] = new int[10];
            System.out.println(a[20]);
            a[5] = 50 / 0;
            
        } catch (ArithmeticException e) {
            System.out.println("Arithmetic Exception ");
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("ArrayIndexOutOfBounds Exception ");
        } catch (Exception e) {
            System.out.println("Parent Exception ");
        }
        System.out.println("Execution done");
    }

Output:

Arithmetic Exception

Execution Done

Another Example of Multiple Catch Blocks

public class MuLtipleCatchBlock{
    public static void main(String[] args) {
        try {
            String s = null;  
            System.out.println(s.length());  
            
        } catch (ArithmeticException e) {
            System.out.println("Arithmetic Exception ");
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("ArrayIndexOutOfBounds Exception ");
        } catch (Exception e) {
            System.out.println("Parent Exception ");
        }
        System.out.println("Execution done");
    }
}

Output:

Arithmetic Exception

Execution Done

Example of Catch Block without maintaining Order

Let’s see an example, to handle the exception without maintaining the order of exceptions.

public class MultipleCatchBlock{
    public static void main(String[] args) {
        try {
            int a[] = new int[10];
            System.out.println(a[20]);
            a[5] = 50 / 0;
        
        catch (Exception e) {
            System.out.println("Parent Exception ");
        }
        } catch (ArithmeticException e) {
            System.out.println("Arithmetic Exception ");
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("ArrayIndexOutOfBounds Exception ");
        } 
        System.out.println("Execution done");
    }

Output:

Compile-time error

Visited 1 times, 1 visit(s) today

Comments are closed.

Close