java - no throws clause -
this program correct , compile , run. why method 'a' not have throws declaration?
class exception1 { public void a() { int array[] = new int[5]; try { system.out.println("try"); array[10]=1; } catch (exception e) { system.out.println("exception"); throw e; } { system.out.println("finally"); return; } } public static void main(string[] args) { exception1 e1 = new exception1(); try { e1.a(); } catch(arrayindexoutofboundsexception e) { system.out.println("catch"); } system.out.println("end of main"); } }
the problem return
in finally
block:
since finally
always executed , always complete abruptly (either unchecked exception or return
), there no way throw e
in catch
-block (or unchecked exception in try
block) ever propagated downwards on call stack.
if remove return
, you'll notice compiler not accept code, stating exception
not declared thrown on method a()
.
Comments
Post a Comment