Java Exception Handling via Catch Block -
this question has answer here:
- exception thrown in catch , clause 10 answers
when executing following code
1. public class test { 2. public static void main(string[] args) { 3. try { 4. int = 10 / 0; 5. } catch (exception e) { 6. int j = 10 / 0; 7. } { 8. int k = 10 / 0; 9. } 10. } 11. }
i getting error:
exception in thread "main" java.lang.arithmeticexception: / 0 @ test.main(test.java:8)
i not getting reason why jvm throwing exception main() , not catch block.
if follow the step-by-step description of try-catch-finally in jls, see (in summary) if catch throws exception executed , (emphasis mine):
if catch block completes abruptly reason r, block executed.
if block completes abruptly for reason s, try statement completes abruptly for reason s (and reason r discarded).
so exception thrown catch block (which executed before block) discarded , reported exception 1 thrown in block.
Comments
Post a Comment