Exception Handling Java -
class chain_exceptions{ public static void main(string args[]){ try { f1(); } catch(indexoutofboundsexception e) { system.out.println("a"); throw new nullpointerexception(); //line 0 } catch(nullpointerexception e) //line 1 { system.out.println("b"); return; } catch (exception e) { system.out.println("c"); } { system.out.println("d"); } system.out.println("e"); } static void f1(){ system.out.println("start..."); throw new indexoutofboundsexception( "parameter" ); } }
i expected line 1 catch nullpointerexception thrown line 0 not happen.
but why ?.
when there catch block defined, why cant npe handler @ line1 catch ?
is because "throw" goes directly main() method ?
catch{ . . . }
blocks associated try{ . . . }
blocks. a catch
block can catch exceptions thrown try block only. other catch blocks after first catch block not associated try block , hence when throw exception, not caught. or main()
not catch exceptions.
a kind of each catch block trying do.
try{ try { f1(); } catch(indexoutofboundsexception e) { system.out.println("a"); throw new nullpointerexception(); //line 0 } } catch(nullpointerexception e) //line 1 { system.out.println("b"); return; }
Comments
Post a Comment