Explanatory Question
Can an exception be rethrown?
Read the answer carefully and go through the related questions on the right side to improve your understanding of this topic.
Yes, an exception can be rethrown.
When an exception is caught inside a catch block, it can be thrown again to be handled by another method (usually the caller method).
This process continues up the call stack until a method is found that can handle the exception properly.
Example:
try { try { int a = 5 / 0; } catch (ArithmeticException e) { System.out.println("Rethrowing exception..."); throw e; // rethrowing the same exception } } catch (ArithmeticException e) { System.out.println("Handled in outer catch block."); }
Output:
Rethrowing exception... Handled in outer catch block.
First read the answer fully, then try to explain it in your own words. After that, open a few related questions and compare the concepts. This method helps you remember the topic for a longer time and improves exam preparation.