YENGtry
From OpenSimulator
(Difference between revisions)
Line 2: | Line 2: | ||
* on the try block we place the code we want to run protected. | * on the try block we place the code we want to run protected. | ||
− | * on the catch block | + | * on the catch block place code to handle the exception |
* on the finally block we place code that should run always. | * on the finally block we place code that should run always. | ||
+ | |||
+ | * throw keyword allows a catch to reissue the exception, so the engine handles it normaly | ||
+ | |||
a try block must be follow by at least one catch or finally block | a try block must be follow by at least one catch or finally block | ||
Line 11: | Line 14: | ||
'''There can not be any llResetScript or osResetAllScripts inside any of this blocks. script will compile but fail with a severe runtime error | '''There can not be any llResetScript or osResetAllScripts inside any of this blocks. script will compile but fail with a severe runtime error | ||
</div><br> | </div><br> | ||
+ | |||
+ | silly example | ||
+ | |||
+ | touch_start(integer nn) | ||
+ | { | ||
+ | integer a = 0; | ||
+ | try | ||
+ | { | ||
+ | llSay(0,"try"); | ||
+ | float c = 1 / a; | ||
+ | } | ||
+ | catch(exception ex) | ||
+ | { | ||
+ | if(yExceptionMessage(ex) == "Division by Zero") | ||
+ | llSay(0,"Where did you learn math?"); | ||
+ | else | ||
+ | throw; | ||
+ | } | ||
+ | finally | ||
+ | { | ||
+ | llSay(0,"finaly"); | ||
+ | } | ||
+ | } |
Revision as of 16:07, 19 September 2020
try, catch and finally allow to handle some exceptions without getting the script killed.
- on the try block we place the code we want to run protected.
- on the catch block place code to handle the exception
- on the finally block we place code that should run always.
- throw keyword allows a catch to reissue the exception, so the engine handles it normaly
a try block must be follow by at least one catch or finally block
there can be several catch blocks one per exception type, but only one finally
There can not be any llResetScript or osResetAllScripts inside any of this blocks. script will compile but fail with a severe runtime error
silly example
touch_start(integer nn) { integer a = 0; try { llSay(0,"try"); float c = 1 / a; } catch(exception ex) { if(yExceptionMessage(ex) == "Division by Zero") llSay(0,"Where did you learn math?"); else throw; } finally { llSay(0,"finaly"); } }