Throw statement

It notifies that an exception has occurred.


Table of contents:


Format

throw

Expression

Semicolon

Example

Format

The format are:


throw ;

throw expression ;


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

If finds the appropriate try statement, it jumps control to the beginning of the catch section. If there is no cathe section, jumps to the next statement of the endtry.

If no 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 returns 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.

If the expression is the reserved variable __error, the __error will be rethrown without being attached.

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.

Copyright © Rice All rights reserved.