Throw statement

The throw statement issues a customized exception.

For a description of exception handling in general, see “Try statement.”

Syntax



Syntax diagram for the throw statement

exceptionExpression
A variable of a Record type that was defined with the Exception stereotype. You can define the variable by referencing a Record type that you create or by referencing a Record type already in use.
Consider the following, customized Record type:
Record CustomerException type Exception
   customerNumber INT;
end
The Record type includes the following fields, which are characteristic of all exceptions:
message
A string that describes the exception.
messageID
A string that contains an ID.
The following code throws the exception:
try
   if (..)
      throw new CustomerException {
         message = "Illegal customer number",
         messageID = "Custom0123",
         customerNumber = currentNumber };
   else 
      ..
   end

onException(except CustomerException)
   // handle the exception

onException (except AnyException)
   // handle other exceptions
end
Here is code for which the exception type is already in use:
nullEx NullValueException;

try
   if (..)
      throw nullEx;
   else
     ..
   end

onException(exception NullValueException)
   // handle the exception

onException (except AnyException)
   // handle other exceptions
end

Compatibility

Table 1. Compatibility
Target Issue
Java No issues
JavaScript No issues.