Exception handling

You can execute the TryFinally control to catch exceptions that occur during script execution:

  • TryFinally

    If an error occurs while executing a statement, a script is typically terminated with an exception.

    If the statement is inside a try-block, the statements in the finally-block are executed before the script is ended.

    Example

    try
    
       InitDatabase("ODIN")
    
    finally
    
       Protocol("Inside Finally-block", 9)
    
    end-try
    
    

  • TryOnError and TryOnErrorFinally

    If an error occurs while executing a statement in a try-block, use the command Raise to skip to the error label defined in OnError.

    Only the first matching OnError block in a try-block is executed.

    Choose the right order of OnError blocks so that specific OnError blocks are executed before general blocks.

    The code in the finally block is executed after the OnError block is processed.

Example

;the special object "Error" contains information about the last error

;If no error occurred - all attributes contain an empty string

;

;Error.Category: the category identifies the sub-systems that threw the error (for example: "OdinBusinessFacade")

;Error.Token: the token is a one-word identifier for the error. Can be specified if you use the Raise function

;Error.Message: contains the error message

Protocol("before try: category '{0}'; error token: '{1}'; error message: '{2}'; ", 0, Error.Category, Error.Token, Error.Message)

;

;modify this value to play with the different OnError code-blocks

a = 10

 

try

   if(a < 10)

      ;This exception will be caught in the OnError("BUSINESS_ERR") block

      Raise("BUSINESS_ERR","a cannot be less than 10 sein")

   end-if

;

   ;This exception will be caught in the OnError() block

   ;because there is no OnError("Err") block

   Raise("Err", "Test-Raise")

;

;

OnError("BUSINESS_ERR")

   ;this code is executed if the exception token is "BUSINESS_ERR"

   ;the exception token is the first parameter of the Raise function

   Protocol("BUSINESS_ERR: {0}", 0, Error.Message)

;

   ;you can also rethrow the error

   ReRaise()

;

OnErrorCategory("OdinBusinessFacade")

   ;this code is executed if the category of the exception is "OdinBusinessFacade"

   Protocol("odin error caught", 0)

;

OnError()

   ;this code is executed if no matching OnError() was found up to this line

   ;information about the exception can be retrieved from the Error object

   Protocol("error category: '{0}'; error token: '{1}'; error message: '{2}'; ", 0, Error.Category, Error.Token, Error.Message)

;

Finally

;

   ;code in the finally block is ALWAYS executed

   ;this is the right place to free resources (database connections, file handles)

   Protocol("Inside Finally-block", 9)

End-try

;

Protocol("after finally: error category: {0}; error token: '{1}'; error message: '{2}'; ", 0, Error.Category, Error.Token, Error.Message)