irScript try/catch/finally blocks are used to implement error handling logic. A try block may be accompanied by a single catch block, a single finally block, or both; it cannot appear by itself:
var x = 0;
try
{
throw 45.5;
}
catch( error )
{
x = error.Value * 2;
}
Errors caught in a catch block are implicitly typed as ScriptError, which has the following
properties:
Property Name | Data type | Read only? | Description |
Class | Value | Yes | Defaults to ‘ScriptError’ |
FromScript | Boolean | Yes | true if the error originates from a irScript throw statement; otherwise false |
Value | <varies> | Yes |
If FromScript is true, Value returns the target of the original throw statement; if FromScript is false, Value returns the CLRexception for the error |
It’s important to note that irScript catch blocks can catch CLR exceptions originating outside irScript proper… for instance, DivideByZeroException or exceptions resulting from CLR interop calls). Try/catch/finally has essentially the same semantics as in the C# language; finally blocks are guaranteed to execute following their matching try block (and any matching catch block):
var x = 0;
try
{
throw 45.5;
}
catch( error )
{
x = error.Value * 2;
}
finally
{
x += 4;
}
One departure from C# is that irScript allows definition of only a single catch block per try; this is primarily due to the implicitly-typed nature of irScript. Handling of multiple error types can be implemented using conditional statements inside a catch block:
var x = CallSomeFunction();
try
{
if ( x.Class != "Integer"
{
throw { ErrorType : "Unexpected type", Value : x };
}
else if( x < 0 )
{
throw { ErrorType : "Unexpected value", Value : x };
}
else
{
// do something with positive integer value…
}
}
catch( error )
{
if( error.FromScript )
{
if( error.ErrorType == "Unexpected type" )
{
x = CallSomeOtherFunction();
}
else
{
x *= -1;
}
}
else
{
throw error; // we decide not to handle CLR errors here…
}
}
Unhandled exceptions cause irScript code to halt immediately; control is transferred to the point
of invocation for the irScript program.
Comments
0 comments
Please sign in to leave a comment.