What Happens If A Try-catch-finally Statement Does Not Have A Catch Clause To Handle An Exception That Is Thrown Within The Body Of The Try Statement

What Happens If A Try-catch-finally Statement Does Not Have A Catch Clause To Handle An Exception That Is Thrown Within The Body Of The Try Statement


  • 16 Mar, 2012

    If a try-catch-finally does not have catch clause to handle exception the exception propagetes to higher level try-catch-statement if any available.

    example1.

    public class ExceptionFun {

        public static void main(String[] args) {
            try {
                System.out.println("inside of outer try/catch body");
                try {
                    System.out.println(1/0);
                }catch(NullPointerException e){
                    System.out.println("Hello Dear!! Exception caught inner try/catch");
                }
            }catch(Exception e){
                System.out.println("Hello Dear!! Exception caught in Outer try/catch");
            }
            
        }

    }

    will output as

    inside of outer try/catch body
    Hello Dear!! Exception caught in Outer try/catch

    Example 2:

    try {
                System.out.println(1/0);
            }catch(NullPointerException e){
                System.out.println("Hello Dear!! Exception received innet try");
            }
            System.out.println("Out side of outer try/catch");

    output:

    Exception in thread "main" java.lang.ArithmeticException: / by zero
    at com.parser.main.Chumma.main(ExceptionFun.java:21)

    Comments Received:
    Please give your suggestions and feedback:
  • 29 Dec, 2011

    The exception propagates up to the next higher level try-catch statement (if any) or results in the program's termination.

    Comments Received:
    Please give your suggestions and feedback:

2009-2016 downloadmela.com. All rights reserved.