<throw>
This statement constructs an exception from its content and throws this exception. If the exception is not caught in a <catch> block within the mashup or macro, this forcibly stops all further mashup or macro processing.
You define the exception as well-formed, literal XML inside <throw> and assign static data or use dynamic mashup expressions to assign data dynamically. See
Creating Custom Exceptions for more information and best practices.
This statement is most commonly used inside a <catch> block within the
<try> statement. You can, however, use it anywhere in a mashup or macro, in looping statements or in other control statements. See
Examples.
Can Contain | |
Allowed In | mashup | catch | else | elseif | for | foreach | if | macro | operation | sequence | try | while |
Creating Custom Exceptions
Custom exceptions are simply well-formed XML documents in this general form:
<exception-classname optional-context-attribute>
<errorcode>number or code</errorcode>
<message>description</message>
</exception-classname>
The exception must contain at least a root node. The name of this root node defines the class of the exception and must be a valid XML name.
Tip: It is a good practice when you throw a custom exception from a mashup or macro to use your own unique exception names rather than using existing exception class names, such as Java exceptions. Reusing exception class names can be confusing and make it more difficult to resolve problems if the exception does occur.
The exception can contain any of the following optional children:
<errorcode> for a numeric or string code that identifies the error which has occurred
<message> with the description of the error
Any other child elements to contain information pertinent to the exception.
An optional attribute of any name on the root node of the custom exception with additional context information such as your organization name, the module or application where this occurred and the name of the mashup or macro that threw this exception.
Tip: It is a best practice to add this additional context as it can help track errors down. This acts much like the fully qualified class name of a Java exception.
The examples in this topic use an attribute named context, but you can use any name you want.
Examples
You can throw custom exceptions to provide better information from <catch> blocks. See
Throwing Custom Exceptions in <catch> for an example.
You can also simply rethrow an exception from a <catch> block:
<try>
<macro:getAdjustedCost height="$this.height" width="$this.width"
outpuvariable="$adjustedCost"/>
<catch type="IllegalArgument">
...
</catch>
<catch type="EMMLException">
<assign literal="1" outputvariable="$defaultWidth"/>
<assign literal="1" outputvariable="$defaultHeight"/>
<!-- and rethrow this exception -->
<throw/>
</catch>
</try>
But you can throw exceptions anywhere within a mashup or macro, such as this example:
...
<input name="maximumWidth" type="number"/>
<input name="maximumHeight" type="number"/>
<input name="unit" type="string"/>
<if condition="not(matches($unit,'ft') or matches($unit, 'yd'
or matches($unit 'm'))">
<throw>
<InvalidMeasurement context="myOrg.adjustEstimateMashup">
<message>The measurement unit must be feet, yards or meters.</message>
</InvalidMeasurement>
</thow>
</if>
...