Next: , Previous: , Up: Exception Handling   [Contents][Index]

14.15.2 try-catch

The try-catch statement provides a way to catch exceptions and handle them.

The simplest form of the statement is:

try stmt catch compound_stmt

where stmt is any statement and compound_stmt is a compound statement. First, stmt is executed. If during its execution an exception is raised, then compound_stmt is executed.

The second form of the statement allows you to catch just one type of exception:

try stmt catch if exp compound_stmt

where exp is an expression that should evaluate to an Exception. The handler compound_stmt will only be executed if that specific exception is caught. Any other exception will be re-raised.

The third form of the statement is the most generic:

try stmt catch (Exception formal) compound_stmt

where formal is a formal argument that contains the exception when compound_stmt is executed.