+ 1
Why does the catch block execute first?
// A Class that represents use-defined expception class MyException extends Exception { public MyException(String s) { // Call constructor of parent Exception super(s); } } // A Class that uses above MyException public class Main { // Driver Program public static void main(String args[]) { try { // Throw an object of user defined exception throw new MyException("GeeksGeeks"); } catch (MyException ex) { System.out.println("Caught"); // Print the message from MyException object System.out.println(ex.getMessage()); } } }
1 Answer
+ 11
What do you mean?
The "try" block is executed first in the "main" function and it throws your custom exception (MyException).
Then the "catch" block is executed, because it is configured to catch your custom exception