×
☰ See All Chapters

try catch in java

try block

  1. Enclose the code that might throw an exception in try block. Code within a try/catch block is referred to as protected code. 

  2. It must be used within the method and must be followed by either catch or finally block. 

  3. If exception doesn’t occurs inside try block, then catch block will be skipped. 

  4. If exception occurs inside try block then remaining statements will be skipped in try block, control jumps to catch block. 

Syntax of try with catch block

try {  

...  

} catch ( Exception_class_Name reference ) {

}  

Syntax of try with finally block

try {  

...  

} finally {

}  

catch block

  1. Catch block is used to handle the Exception. It must be used after the try block. 

  2. A catch statement involves declaring the type of exception you are trying to catch. If an exception occurs in protected code, the catch block (or blocks) that follow the try is checked. If the type of exception that occurred is listed in a catch block, the exception is passed to the catch block much as an argument is passed into a method parameter. If you say you want to catch a NullPointerException and an ArithmeticException occurs, you will not catch the ArithmeticException. 

  3. If you say you want to catch an Exception, then you will catch every exception that might arise. Remember, all exceptions are child classes of Exception, so through polymorphism, all exceptions are of type Exception. 

try-catch-in-java-0
 
try-catch-in-java-1
 
try-catch-in-java-2
 

Multiple catch blocks:

  1. One try block can have multiple catch statements. In some cases, more than one exception can occur in a try block, but always only one type of an exception object will be created at a time. To handle such situations one try block can have multiple catch statements. But since always only one type of an exception object will be created in the try block, only one catch block gets executed. 

  2. When an exception is thrown, each catch statement is inspected in order and the first one which matches that exception type will get executed. 

  3. All catch blocks must be ordered from most specific to most general i.e. catch for ArithmeticException must come before catch for Exception. 

  4. Even though class Exception can handle all the exceptions, it is always advisable to write the specific type of Exception in the catch block. Because after catching the Exception, next we have to check the type of Exception which has occurred, and then write the logic to fix the error for that exception. 

try-catch-in-java-3
 

Nested try block

Nested try block is a try block within a try block is known as nested try block. Sometimes a situation may arise where a part of a block may cause one error and the entire block itself may cause another error. In such cases, exception handlers have to be nested

Nested try block syntax

...  

try {  

    statement 1;  

    statement 2;  

    try {  

        statement 1;  

        statement 2;  

    }  

    catch(Exception e) {  

    }  

}  

catch(Exception e) {  

}  

....  

Nested try block Example

class ExceptionHandling {

        public static void main(String args[]) {

                try {

                        try {

                                System.out.println("going to divide");

                                int b = 39 / 0;

                        } catch (ArithmeticException e) {

                                System.out.println(e);

                        }

                        try {

                                int a[] = new int[5];

                                a[5] = 4;

                        } catch (ArrayIndexOutOfBoundsException e) {

                                System.out.println(e);

                        }

                        System.out.println("other statement");

                } catch (Exception e) {

                        System.out.println("handeled");

                }

                System.out.println("normal flow..");

        }

}

Output:

going to divide

java.lang.ArithmeticException: / by zero

java.lang.ArrayIndexOutOfBoundsException: 5

other statement

normal flow..

 


All Chapters
Author