java - Exception Handling Unreachable code -
following code, when commenting statement-2 complies fines when uncomment gives compile time error "unreachable code"
.
i understand why getting error after uncommenting it, question if comment still bad()
unreachable throwing
exception catch why not giving error ?
class varr { public static void main(string[] args) throws exception { system.out.println("main"); try { good(); } catch (exception e) { system.out.println("main catch"); //**statement 1** throw new runtimeexception("re"); } { system.out.println("main finally"); // **statement 2** throw new runtimeexception("re2"); } bad(); } }
but question if comment still bad() unreachable throwing exception catch why not giving error ?
because execution not necessary enter in catch
statement.
suppose good()
doesn't thrown exception, don't enter in catch
, therefore bad()
executed :
public static void main(string[] args) throws exception { system.out.println("main"); try { good(); // doesn't throw exception } catch (exception e) { system.out.println("main catch"); throw new runtimeexception("re"); } bad(); // execution goes good() here }
Comments
Post a Comment