Home News DOC Class DL Others Cooker
Documents  >  Tutorial  >  Throw
Throw statement

It notifies that an exception has occurred.


Table of contents:


Format

throw

Expression

Semicolon

Example

Format

The format of the throw statement are:


throw ;

throw expression ;


It throws an instance of the error class to signal that an exception has occurred. As a result, execution control goes back the call history in order to find the nearest try statement surrounding the throw statement.

When it finds the appropriate try statement, it jumps control to the beginning of the section if there is a catch section, or to the next statement of the endtry if it doesn't.

If no such try statement is found, an exception arrives at the application and the Rice program stops executing.

throw

A keyword that indicates that it is a throw statement.

Expression

The instance that the expression generates is attached to the instance of the error class. If you don't need the instance, you can omit it.


If the throw statement has no expression, the ExceptionData getter of the error class is an instance of the uninitialized proxy class.

If the throw statement has an expression, the ExceptionData getter of the error class is an instance of the proxy class initialized with the expression.

The exception is that if the expression is the reserved variable __error, __error is rethrown.

Semicolon

The end of a throw statement is represented by a semicolon.

Example

1:

class example

2:

open method void someMethod()

3:

throw "exception";

4:

em

5:

ec

1:

example ex;

2:

try

3:

ex.someMethod(); // It throws an exception.

4:

catch

5:

string mess = __error.ExceptionData.Entity; // "exception"

6:

endtry

An exception is thrown from the someMethod() method. Since the throw statement has an string literal, the ExceptionData getter of the thrown instance is initialized with a string.


The exception is caught in the try statement surrounds the method call. Execution control is moved because the try statement has a catch section.

Thrown instance is stored to the reserved variable __error when execution control is moved to the catch section.

Next
Previous
Copyright © Rice All rights reserved.