java - Exception Handling flow discrepancy -
my code below :-
class varr { public static void main(string[] args) { try { system.out.println(10/0); } catch(arithmeticexception e) { system.out.println("catch1"); system.out.println("catch1"); throw new arithmeticexception ("exce"); } { system.out.println("finally"); } } }
output :-
catch1 catch1 catch1 exception in thread "main" java.lang.arithmeticexception: exce @ one.varr.main(varr.java:22)
as per knowledge flow has first try catch , @ last per output flow try few lines of catch upto throw exception statement , , throw exception statement of catch block @ last.
why there discrepancy in flow, mean why executed before throw new exception statement of catch block
because block of finally
, definition, has executed no matter outcome of try
or catch
clauses.
in case, run-time knows there exception has propagated upwards, before doing so, executes whatever inside finally
block.
when finally
block finished, propagates exception might have been raised, or flow continues otherwise.
Comments
Post a Comment