previous | start | next

Syntax 15.3: General Try Block

 
try
{
   statement
   statement
   . . . 
} 
catch (ExceptionClass exceptionObject)
{ 
   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.


previous | start | next