Tuesday, July 23, 2019

Exception Handling Examples In Java

Q- What is the output of below program?

package com.shubh.example;

public class ExceptionExample {

 public static void main(String[] args) {
  try {
   execute();
   System.out.println("Step 3- Inside main method.");
  } catch (Exception e) {
   System.out.println("Step 4- Inside main catch block.");
  } finally {
   System.out.println("Step 5- Inside main finally block.");
  }
 }

 public static void execute() {
  try {
   throw new IllegalArgumentException("thorw IllegalArgumentException.");
  } catch (Exception e) {
   System.out.println("Step 1 - Inside execute catch block.");
  } finally {
   System.out.println("Step 2 -Inside execute finally block.");
  }
 }

}
Output:

Step 1 - Inside execute catch block.
Step 2 -Inside execute finally block.
Step 3- Inside main method.
Step 5- Inside main finally block.
Q- What is the output of below program.

package com.shubh.example;

public class CustomExceptionExample {
 
 public static void main(String[] args) {
  try {
   throw new Test();
   System.out.println("Step 1- Inside main method.");  // line no. 8
  } catch (Exception e) {
   System.out.println("Step 2- Inside main catch block.");
  } finally {
   System.out.println("Step 3- Inside main finally block.");
  }
 }
}

class Test extends Exception {
 
}
Output:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
 Unreachable code

 at com.shubh.example.CustomExceptionExample.main(CustomExceptionExample.java:8)
Q- What is the output of below program.

package com.shubh.example;

public class CustomExceptionExample {
 
 public static void main(String[] args) {
  try {
   throw new Test();
  } catch (Exception e) {
   System.out.println("Step 1- Inside main catch block.");
  } finally {
   System.out.println("Step 2- Inside main finally block.");
  }
 }
}

class Test extends Exception {
 
}
Output:

Step 1- Inside main catch block.
Step 2- Inside main finally block.
Q- What is the output of below program?

package com.shubh.example;

public class CustomExceptionExample2 {
 
 public static void main(String[] args) {
  try {
   throw new Test();
  } catch (Exception e) {
   System.out.println("Step 1- Inside main catch block.");
  } finally {
   System.out.println("Step 2- Inside main finally block.");
  }
 }
}

class Test extends Exception {
 public Test() {
        super();
    }
}
Output:

Step 1- Inside main catch block.
Step 2- Inside main finally block.

Q- What is the output on below code?
package com.exception;

public class ExceptionExample {

 public static void main(String[] args) {

  try {
   execute();
   System.out.println("Success");
  } catch (Exception e) {
   e.printStackTrace();
  } finally {

   System.out.println("Finally call.");

  }
 }

 public static void execute() {
  try {
   throw new ArithmeticException();
  } catch (Exception e) {
   throw new IllegalArgumentException("thorw IllegalArgumentException.");
  } finally {
   throw new ArrayIndexOutOfBoundsException(); // Line no 25
  }
 }
}
Output:
java.lang.ArrayIndexOutOfBoundsException
 at com.exception.ExceptionExample.execute(ExceptionExample.java:25)
 at com.exception.ExceptionExample.main(ExceptionExample.java:8)
Finally call.

No comments:

Post a Comment