Try-Catch Blocks
The statements that immediately follow the try statement are called the protected statements.. These are the only statements that are “protected” by the catch clauses in the try-catch blocks. The catch clauses in a try-catch block can be executed only if an exception is thrown by the protected statements. In addition, a catch clause is executed only when handling an exception that matches the type given on the catch. Any exceptions thrown by the catch clauses are not caught by their own try-catch block.
The execution of the try-catch block starts by executing the protected statements. If none of these statements—as well as none of the statements called by them—causes an exception to be thrown, the try-catch block is done. The statements in the catch clauses are not executed.
Each catch clause in the try-catch section declares a local variable of class Exception or a subclass of Exception. A catch clause local variable has the same scope and lifetime as a local variable declared at the start of the catch clause.
The following is the general order of exception execution and handling in try-catch blocks:
The exception is thrown.
The exception is considered by the first enclosing try-catch block.
Try-catch blocks can be nested.
This is a dynamic enclosure—that is, tracking back through method and function callers. Any intervening non-try-catch blocks are skipped.
Within this try-catch block, the catch clauses are considered in program text order—that is, first the first catch clause is considered, then the next, and so on to the last. At each consideration, the type (class) of the exception being thrown is compared to the type of the local variable declared by the catch clause. If the exception being thrown is the same as the catch type or is a subtype of it (so the exception could be assigned to the catch's local variable), the catch clause matches the exception being thrown.
If a matching catch clause is found, that catch clause handles the exception. The exception is considered “caught,” which means that it does not propagate further. Execution proceeds by assigning the thrown object to the catch clause’s local variable and executing the statements in the catch clause. When one of the catch clauses handles the exception (and the statements in the catch clause don’t throw any further exceptions), execution continues normally after the try-catch block.
If a statement in the catch clause throws another exception, that exception begins to propagate from the point of the throw.
If a matching catch clause is not found—that is, if no catch clauses can accept the thrown object—then the thrown object is still uncaught. The thrown object is considered by the next dynamically enclosing try-catch block (return to step 3).
If a thrown object is not caught by any catch clause in any dynamically enclosing try-catch blocks, a fatal PeopleCode evaluation error is produced.