Home About Rice Downloads Documents Others Cooker CookerGX
Documents  >  Tutorial  >  trystatement
try statement

Purpose of this chapter.

We will learn about the try statement.

Capturing of the exception.

A try statement is a statement to branch a flow of execution depending on exception.

A try statement is the following format.


try

statements...

catch

statements...

endtry


If an exception has occurred in a try section, it operates as follows.

If a catch section is exist, the execution control will move to the first statement in the catch section.

If a catch section is not exist, the execution control will move to the next statement of endtry keyword.

If an exception did not occur in the try section, a catch section is skipped, and the execution control will move to the next statement of endtry keyword.

An try statement have to end with the endtry keyword. The catch section is optional.

try keyword

The try keyword indicates that this statement is a try statement.

The end of the try statement.

The end of the try statement is represented by an endtry keyword.

try section

The try section is located to the first of the try statement. Its range is up to the next keyword (catch, endtry) of the same statement.

The try section is the following format.


try

statements...


If an exception is thrown in the try section, the execution control will move to the next statement of the catch keyword when there is a catch section. Otherwise, the execution control will move to the next statement of the endtry keyword.

If the execution control reaches the next keyword (catch, endtry) of the same statement, the execution control will move to the next statement of the endtry keyword.

The try section must be only one at the beginning of the try statement.

try keyword

The try keyword indicates that this section is a try section.

It represents a try statement and try section at the same time.

Scope

The try section will generate a scope. Variable that has been declared in the section can be referred only within the section.

catch section

The catch section is located to the end of the try statement. Its range is up to the next endtry keyword of the same statement.

The catch section is the following format.


catch

statements...


If an exception is thrown in the try section, statements in the section will be executed unconditionally.

If the execution control reaches the endtry keyword of the same statement, the execution control will move to the next statement of the endtry keyword.

The catch section is optional. And, it cannot exist two or more.

Scope

The catch section will generate a scope. Variable that has been declared in the section can be referred only within the section.

Reserved variable.

If the control reaches to the catch keyword, a scope will generate. And, a try statement automatically defines the reserved variable __error.

__error is a variable of the error class. It represents an exception caught by a try statement.

Next
Previous
Copyright © Rice All rights reserved.