YENGtry

From OpenSimulator

(Difference between revisions)
Jump to: navigation, search
(string yExceptionTypeName(exception ex))
Line 54: Line 54:
 
     }
 
     }
 
  }
 
  }
 +
 +
but catch(scriptexception ex) is faster and more readable
  
 
= Examples =
 
= Examples =

Revision as of 05:39, 21 September 2020

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 cache 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 auxiliar functions

string yExceptionMessage(exception ex)

returns a string with the message of the exception ex

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

Examples

silly example

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

detect no ossl permission

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