Java Exception Handling via Catch Block -


this question has answer here:

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

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -