http://wiki.dreamnation.net/index.php?title=XMREngine.trycatch&feed=atom&action=historyXMREngine.trycatch - Revision history2024-03-28T22:23:51ZRevision history for this page on the wikiMediaWiki 1.24.1http://wiki.dreamnation.net/index.php?title=XMREngine.trycatch&diff=15&oldid=prevAdmin: Created page with " == EXCEPTION HANDLING == Provide a mechanism for catching exceptions. Enabled by: xmroption trycatch; '''Keywords''' catch exception finally throw t..."2015-02-03T21:56:30Z<p>Created page with " == EXCEPTION HANDLING == Provide a mechanism for catching exceptions. Enabled by: xmroption trycatch; '''Keywords''' catch exception finally throw t..."</p>
<p><b>New page</b></p><div><br />
== EXCEPTION HANDLING ==<br />
<br />
<br />
Provide a mechanism for catching exceptions.<br />
<br />
Enabled by: xmroption trycatch;<br />
<br />
'''Keywords'''<br />
<br />
catch<br />
exception<br />
finally<br />
throw<br />
try<br />
<br />
'''Statements'''<br />
<br />
'try' '{' statements ... '}'<br />
[ 'catch' '(' 'exception' name ')' '{' statements ... '}' ]<br />
[ 'finally' '{' statements ... '}' ]<br />
<br />
'throw' expression ';'<br />
<br />
'throw' ';' // rethrow from within a 'catch' clause<br />
<br />
A single try can include any number (greater than zero) of catch and/or <br />
finally clauses, in any order. They are stepped through one-by-one and <br />
executed as needed. try/catch/finally can be nested in any combination.<br />
<br />
The expression of a throw statement within a try clause can be of any type, <br />
eg, integer, string, script-defined object type. Any finally clauses <br />
between the throw and the corresponding catch are executed before the <br />
catch. Then the single catch is executed. If the catch rethrows the <br />
execption, then the search for a catch clause resumes, executing finally <br />
clauses until a match is found. If a catch does not rethrow the execption, <br />
all finally clauses outside the catch are executed and all catch clauses <br />
outside the catch are ignored.<br />
<br />
If an exception is thrown and no catch clause is found, all finally clauses <br />
are executed in order and the script is terminated and an error message is <br />
printed. This also applies to an exception thrown outside any try clause <br />
trivially as there can't be any catch clauses for it and there won't be any <br />
finally clauses to execute either, the script is simply terminated and an <br />
error message is printed.<br />
<br />
'''Related built-in functions'''<br />
<br />
string xmrExceptionMessage(exception)<br />
<br />
get an human readable error message associated with the exception.<br />
<br />
string xmrExceptionStackTrace(exception)<br />
<br />
gets a string indicating where in the script the exception occurred.<br />
<br />
object xmrExceptionThrownValue(exception)<br />
<br />
given the value from the catch clause, return the object from the <br />
throw statement that threw the exception. If the exception was not <br />
from a throw statement (ie, some internal exception), this call <br />
throws an "InvalidCastException".<br />
<br />
string xmrExceptionTypeName(exception)<br />
<br />
given the value from the catch clause, return a string giving the <br />
type of the exception, eg, "ScriptThrownException" for a throw <br />
statement. Other types can be "NullReferenceException", etc.<br />
<br />
'''Example'''<br />
<br />
xmroption arrays;<br />
xmroption trycatch;<br />
state_entry ()<br />
{<br />
try {<br />
CallSomethingThatThrows ("try to catch this!");<br />
} finally {<br />
llOwnerSay ("finished one way or another");<br />
} catch (exception ex) {<br />
PrintOutException (ex);<br />
}<br />
<br />
try {<br />
llOwnerSay (((array)(object)undef).count);<br />
} catch (exception ex) {<br />
PrintOutException (ex);<br />
}<br />
}<br />
}<br />
<br />
CallSomethingThatThrows (string s)<br />
{<br />
llOwnerSay ("say something so we don't get inlined");<br />
throw s;<br />
}<br />
<br />
PrintOutException (exception ex)<br />
{<br />
llOwnerSay (" typename: " + xmrExceptionTypeName (ex));<br />
llOwnerSay (" message: " + xmrExceptionMessage (ex));<br />
try {<br />
object tv = xmrExceptionThrownValue (ex);<br />
llOwnerSay ("thrownvalue: " + tv);<br />
} catch (exception ex2) {<br />
if (xmrExceptionTypeName (ex2) != "InvalidCastException") throw;<br />
}<br />
llOwnerSay (" stacktrace:\n" + xmrExceptionStackTrace (ex));<br />
}<br />
<br />
...generates this output:<br />
<br />
say something so we don't get inlined<br />
finished one way or another<br />
typename: ScriptThrownException<br />
message: try to catch this!<br />
thrownvalue: try to catch this!<br />
stacktrace:<br />
at CallSomethingThatThrows(string) <trycatchexample.lsl(27,4)><br />
at default state_entry <trycatchexample.lsl(9,36)><br />
<br />
typename: NullReferenceException<br />
message: Object reference not set to an instance of an object<br />
stacktrace:<br />
at default state_entry <trycatchexample.lsl(17,23)></div>Admin