throw

The throw keyword throws an exception.

Purpose

This statement is used in the action part of rules or in functions to throw an exception that makes control flow immediately to an exception handler.

Context

Functions or rule actions

Syntax

throw 
expression

Description

The throw statement requires a single expression that is a throwable object. Throwable objects are instances of any subclass of the Java™ Throwable class.

The exception in the throw statement is not the exception actually thrown. The thrown exception is an instance of the IlrUserRuntimeException class, a subtype of IlrRuntimeException, which encapsulates the exception thrown in the throw statement. The inherited IlrRuntimeException#getTargetException() accessor retrieves the exception thrown in the throw statement. This method has the following signature: public Throwable getTargetException()

Example

function void replaceValue(String name, Object newValue)
{
   String ?attr = find(name); 
   if (?attr == null)       
      throw new NoSuchAttributeException(name,this);
   ?attr.valueOf(newValue);
}

This example shows a function that replaces the attribute valueOf by an object called newValue. The function assigns the name string to an attr object and then tests whether the attr object can be found. If the attr object does not exist, the NoSuchAttributeException is thrown. The thrown exception can be caught either in Java code by catching an IlrUserRuntimeException or superclass object, or in an IRL rule or function by catching a NoSuchAttributeException or superclass object.

Note that, unlike the Java code, the declaration of the replaceValue function does not contain a throws statement specifying that the function can throw a NoSuchAttributeException instance.