Java Reference
In-Depth Information
statement
statement
. . .
}
catch (ExceptionClass exceptionObject)
{
statement
statement
. . .
}
. . .
Example:
try
{
System.out.println("How old are you?");
int age = in.nextInt();
System.out.println("Next year, you'll be " +
(age + 1));
}
catch (InputMismatchException exception)
{
exception.printStackTrace();
}
Purpose:
To execute one or more statements that may generate exceptions. If an exception
occurs and it matches one of the catch clauses, execute the first one that
matches. If no exception occurs, or an exception is thrown that doesn't match any
catch clause, then skip the catch clauses.
509
510
If any of these exceptions is actually thrown, then the rest of the instructions in the
try block are skipped. Here is what happens for the various exception types:
ȗ
If a FileNotFoundException is thrown, then the catch clause for the
IOException is executed. (Recall that FileNotFoundException is a
subclass of IOException .)
ȗ
If a NumberFormatException occurs, then the second catch clause is
executed.
Search WWH ::




Custom Search