In this section we will how to use try-catch block in our program.

  • try block

    Write code inside this block which could generate errors during the execution of program.
    Inside try block we write the error prone statements which can cause error during execution of program.
    It must be used within the method and must be followed by either catch or finally block.


    Syntax of try with catch block

    
    try{
    //code that may throw exception  
    }catch(Exception_class_Name    reference)
    {
    //Handling Code
    }


    Syntax of try with finally block

    try{  
    //code that may throw exception  
    }finally{
    //code which we want to execute always
    }  

  • catch block

    Catch block is used to handle the Exception. It must be used after the try block.
    You can use multiple catch block with a single try.

  • Problem without exception handling

    Let's try to understand the problem if we don't use try-catch block.

    Example)
    drag_handle
     
    //example of problem without exception handling
    class Example
    {
        public static void main(String[] args) {
            System.out.println("Starting of the program....");
            int a=34;
            int b=0;
            int c=a/b; //error prone statement..
            System.out.println("Division is = "+c);
            System.out.println("Ending of the program........");
        }
    }
    Output:
    Starting of the program....
    Exception in thread "main" java.lang.ArithmeticException: / by zero

    As displayed in the above example, rest of the code is not executed i.e. 'ending of program ' statement is not printed. this is what abnormal termination of the program is . it is not good for our java application. try to void this type of exceptions by using try-catch block/handling mechanism .

    //important point

  • Solution by exception handling

    Let us try to solve the above problem by using Exception Handing/try-catch block.
    Solution :put the error prone code in try-block and put the handling code in catch-clock.
    Let us see the example :

    Example)
    drag_handle
     
    //example of handling Arithmetic Exception
    class Example
    {
        public static void main(String[] args) {
            System.out.println("Starting of the program....");
            int a=34;
            int b=0;
            try
            {
            int c=a/b; //error prone statement..
            System.out.println("Division is = "+c);
            }catch(ArithmeticException e)
            {
                //handling code here
                System.out.println("Run time error has occurred.");
                System.out.println(e.getMessage());
            }
           
            System.out.println("Ending of the program........");
        }
    }
    Output:
    Starting of the program....
    Run time error has occurred.
    / by zero
    Ending of the program........
    
  • Internal working of java try-catch block

    Let's see what happens behind the scene:

    Internal working of java try-catch block
  • Note :

    The JVM firstly checks whether the exception is handled or not. If exception is not handled, JVM provides a default exception handler that performs the following tasks:

    • Prints the stack trace (Hierarchy of methods where the exception occurred).

    • Causes the program to terminate.

    • Prints out exception description.

    • But if exception is handled by the application programmer, normal flow of the application is maintained i.e. rest of the code is executed.

What next?

The next topic is Multiple catch block in java

Share this page