Why does the catch block execute first? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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()); } } }

5th Apr 2021, 12:48 PM
kreddyt
kreddyt - avatar
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
5th Apr 2021, 3:14 PM
Igor Makarsky
Igor Makarsky - avatar