Exception-handling statements
- Last UpdatedApr 24, 2024
- 1 minute read
XRS Script language supports C-like statements for exception handling:
-
throw
-
try
-
catch
-
finally
Currently, only one catch and one exception is supported, but a nested try block can be created.
throw
Throw statement is used to throw an exception.
When an exception is thrown, the catch block that can handle the exception is searched. If the currently executed block doesn't contain a catch block, search continues up the call stack. The try block execution is ended, if no catch block is found.
try
The try statement can be used in any of the following forms: try-catch, try-finally, try-catch-finally
catch
The catch statement is used to handle exceptions that might occur during code execution inside the try block. A catch clause can be added to specify the type of exception you want to handle in the corresponding catch block.
The throw statement can be used inside a catch block to rethrow a caught exception.
finally
The finally statement is used to specify the code executed when execution exits the try or catch block. Execution can leave the try block after normal execution or propagation of an exception out of the try block. The only cases where finally block isn’t executed are when program is terminated immediately.
Example code
static void testTryCatch()
{
try
{
Runtime::Environment.Print("inside trycatch");
try
{
throw new ExceptionA("MessaggioA");
// throw new Exception(“ExceptionA“,"MessaggioA");
}
catch(Exception ex2)
{
throw ex2;
}
}
catch(Exception ex)
{
Runtime::Environment.Print(ex.message+" "+ex.line);
Runtime::Environment.Print(ex);
}
finally
{
Runtime::Environment.Print("finally executed");
}
}