Why can't the "Statements following the structure" be used to close a file that the method has opened?
These statements will not execute if the try{}
block throws an
unhandled exception.
If the last catch block catches Exception
,
then it will catch any specific Exception
not caught
in the preceeding blocks.
Do this so the user sees a pleasant error message rather than a
confusing stack trace.
Here is part of the program:
try { System.out.print("Enter the numerator: "); num = scan.nextInt(); System.out.print("Enter the divisor : "); div = scan.nextInt(); System.out.println( num + " / " + div + " is " + (num/div) + " rem " + (num%div) ); } catch (ArithmeticException ex ) { System.out.println("You can't divide " + num + " by " + div); } catch (Exception ex ) { System.out.println("Something went wrong." ); } System.out.println("Good-by" );
Now specific arithmetic exceptions are caught in the first block, and everything else in the other.
Where will a RunTimeException
be caught?