java - Why is the finally statement needed if there is no catch block? -
i know try
statement useless without catch
or finally
, , finally
clause optional in try-catch
block. however, when write try
statement without catch
or finally
, compiler suggests inserting finally
clause complete try statement.
for example:
try { (int j = 0; j <= i.length; j++) { system.out.println(i[j]); } } catch (arrayindexoutofboundsexception e) { system.out.println("catch"); } //no errors try { (int j = 0; j <= i.length; j++) { system.out.println(i[j]); } } //syntax error
error code:
exception in thread "main" java.lang.error: unresolved compilation problem: syntax error, insert "finally" complete trystatement @ driver.main(driver.java:12)
why finally
recommended statement implement? why not catch
?
"i might want happen despite exception being thrown, may not looking handle specific exception in specific way, may want ensure @ least generic happens. if can't handle it, @ least something." looking confirm on this.
because try
without catch
or finally
makes no sense @ all. nothing you'd have same result if omit try-block.
Comments
Post a Comment