YENGtry

From OpenSimulator

(Difference between revisions)
Jump to: navigation, search
m
 
(11 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 +
<div style="background-color:#FFA0A0; padding:10px; padding-bottom:5px; border: 1px #FF544F solid">
 +
'''There can not be any llResetScript, osResetAllScripts or llDie inside any of this blocks. script will compile but fail with a severe runtime error
 +
</div><br>
 +
 
try, catch and finally allow to handle some exceptions (see [[runtime exceptions]]) without getting the script killed.
 
try, catch and finally allow to handle some exceptions (see [[runtime exceptions]]) without getting the script killed.
  
* 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 a catch block we place code to handle the exception
+
* On a catch blocks we place code to handle the exceptions
* on the finally block we place code that should run always.
+
* On the finally block we place code that should always run.
 
+
 
* throw keyword allows a catch to reissue the exception, so the engine handles it normally
 
* throw keyword allows a catch to reissue the exception, so the engine handles it normally
  
 
+
A try block must be follow by at least
a try block must be follow by at least
+
 
  one or two of the following catch types (possible more in future),
 
  one or two of the following catch types (possible more in future),
 
  one finally block
 
  one finally block
Line 14: Line 16:
  
 
<div style="background-color:#FFA500; padding:10px; padding-bottom:5px; border: 1px #FF544F solid">
 
<div style="background-color:#FFA500; padding:10px; padding-bottom:5px; border: 1px #FF544F solid">
'''at this time there are only 2 types of cache blocks
+
'''at this time there are only 2 types of catch blocks
  
 
to catch script related exceptions
 
to catch script related exceptions
Line 24: Line 26:
 
catch(exception ex) { }
 
catch(exception ex) { }
  
 
+
in both cases particular exception must be found by looking to the message.<br>
 
+
The catch blocks types are checked by their order in source.<br>
in both cases particular exception must be looking to the message.<br>
+
Only the first that matches the exception type is executed, so if both needed scriptexception should be first.<br>
the catch blocks types are check by their order in source, if both needed scriptexception should be first.<br>
+
 
</div><br>
 
</div><br>
  
<div style="background-color:#FFA0A0; padding:10px; padding-bottom:5px; border: 1px #FF544F solid">
+
There are a few auxiliary functions.
'''There can not be any llResetScript, osResetAllScripts or llDie inside any of this blocks. script will compile but fail with a severe runtime error
+
</div><br>
+
 
+
there are a few auxiliar functions
+
  
 
= string yExceptionMessage(exception ex) =
 
= string yExceptionMessage(exception ex) =
Line 46: Line 43:
 
so catch(scriptexception ex) could be
 
so catch(scriptexception ex) could be
  
catch(exception ex)
+
<source lang="lsl">
{
+
catch(exception ex)
 +
{
 
     if(yExceptionTypeName(ex) == "ScriptException")
 
     if(yExceptionTypeName(ex) == "ScriptException")
 
     {
 
     {
    ...
+
        ...
 
     }
 
     }
}
+
}
 +
</source >
 +
 
 +
but catch(scriptexception ex) is faster and more readable
  
 
= Examples =
 
= Examples =
silly example
+
Make sure to add yoptions; to the second line of the script, usually line number 1 starting from 0.
  
touch_start(integer nn)
+
Silly example.
{
+
 
 +
<source lang="lsl">
 +
touch_start(integer nn)
 +
{
 
     integer a = 0;
 
     integer a = 0;
 +
 
     try
 
     try
 
     {
 
     {
      llSay(0,"try");         
+
        llSay(PUBLIC_CHANNEL,"try");         
      float c = 1 / a;
+
        float c = 1 / a;
 
     }
 
     }
 
     catch(scriptexception ex)
 
     catch(scriptexception ex)
 
     {
 
     {
 
         if(yExceptionMessage(ex) == "Division by Zero")
 
         if(yExceptionMessage(ex) == "Division by Zero")
          llSay(0,"Where did you learn math?");
+
            llSay(PUBLIC_CHANNEL,"Where did you learn math?");
 
         else
 
         else
        throw;
+
            throw;
 
     }
 
     }
 
     finally
 
     finally
 
     {
 
     {
      llSay(0,"finaly");
+
        llSay(PUBLIC_CHANNEL,"finaly");
 
     }
 
     }
}
+
}
 +
</source >
  
detect no ossl permission
+
Detect no ossl permission.
  
touch_start(integer nn)
+
<source lang="lsl">
{
+
touch_start(integer n)
    try
+
{
    {
+
    try
 +
    {
 
         key agent = llDetectedKey(0);
 
         key agent = llDetectedKey(0);
 
         osForceOtherSit(agent);
 
         osForceOtherSit(agent);
    }
+
    }
    catch(scriptexception ex)
+
    catch(scriptexception ex)
    {
+
    {
        string message = yExceptionMessage(ex);
+
        string message = yExceptionMessage(ex);
        if(osStringStartsWith(message,"ossl permission error", TRUE))
+
        if(osStringStartsWith(message,"ossl permission error", TRUE))
             llSay(0,"You need to enable  osForceOtherSit on osslEenable.ini");
+
             llSay(PUBLIC_CHANNEL, "You need to enable  osForceOtherSit on osslEenable.ini");
        else
+
        else
 
             throw;
 
             throw;
    }
+
    }
}
+
}
 +
</source >
 +
 
 +
[[Category:Scripts]]

Latest revision as of 19:15, 12 October 2022

There can not be any llResetScript, osResetAllScripts or llDie inside any of this blocks. script will compile but fail with a severe runtime error


try, catch and finally allow to handle some exceptions (see runtime exceptions) without getting the script killed.

  • On the try block we place the code we want to run protected.
  • On a catch blocks we place code to handle the exceptions
  • On the finally block we place code that should always run.
  • throw keyword allows a catch to reissue the exception, so the engine handles it normally

A try block must be follow by at least

one or two of the following catch types (possible more in future),
one finally block
all of those

at this time there are only 2 types of catch blocks

to catch script related exceptions

catch(scriptexception ex) { }

to catch any available exception a script can intercept:

catch(exception ex) { }

in both cases particular exception must be found by looking to the message.
The catch blocks types are checked by their order in source.
Only the first that matches the exception type is executed, so if both needed scriptexception should be first.


There are a few auxiliary functions.

[edit] string yExceptionMessage(exception ex)

returns a string with the message of the exception ex

[edit] string yExceptionTypeName(exception ex)

returns a string with system name of the exception. scriptexception will return ScriptException

so catch(scriptexception ex) could be

catch(exception ex)
{
    if(yExceptionTypeName(ex) == "ScriptException")
    {
        ...
    }
}

but catch(scriptexception ex) is faster and more readable

[edit] Examples

Make sure to add yoptions; to the second line of the script, usually line number 1 starting from 0.

Silly example.

touch_start(integer nn)
{
    integer a = 0;
 
    try
    {
        llSay(PUBLIC_CHANNEL,"try");        
        float c = 1 / a;
    }
    catch(scriptexception ex)
    {
        if(yExceptionMessage(ex) == "Division by Zero")
            llSay(PUBLIC_CHANNEL,"Where did you learn math?");
        else
            throw;
    }
    finally
    {
        llSay(PUBLIC_CHANNEL,"finaly");
    }
}

Detect no ossl permission.

touch_start(integer n)
{
    try
    {
        key agent = llDetectedKey(0);
        osForceOtherSit(agent);
    }
    catch(scriptexception ex)
    {
        string message = yExceptionMessage(ex);
        if(osStringStartsWith(message,"ossl permission error", TRUE))
            llSay(PUBLIC_CHANNEL, "You need to enable  osForceOtherSit on osslEenable.ini");
        else
            throw;
    }
}
Personal tools
General
About This Wiki