Error handling
Looking for the documentation on the newest versions of SIL Engine and the Simple Issue Language for Jira 8 for Server/Data Center?Ā Click hereĀ !
Contents
Even though we've tried to keep the language as user-friendly as possible by returning empty values rather than throwing exceptions where possible, there are still cases when exceptions need to be thrown.
Additionally, users may want to throw the errors themselves (for instance from common user-defined functions) and handle error results differently depending on context.
There are two types of exceptions:
- Java Exceptions - thrown from the java code running behind SILā¢
- SILā¢ objects thrown usingĀ throw (explained below)
Throwing SIL Objects
The general syntax for throwing an exception from SILā¢ is:
throw <value>;
Any SILā¢ value can be thrown as exception. Values can either be constants, variables or expressions.
throw errorCode; throw 2; throw array[0].fieldName; throw ("Fiend value invalid: " + structure.field); throw "Generic error!";
Try-catch block
try { <instructions>Ā } catch <type> <varName> { // handle <varName> } catch { // handle other error }
The try-catch block contains the following components:
- one try blockĀ containing instructions to execute.Ā
- zero, one or more typedĀ catch clauses.
- zero or one catch all clauseĀ
If any instruction in the try block throws an exception, execution of instructions within the try block is stopped. That is, any instructions within the try block after the one that throws the exception will not be executed.Ā
Next, SILā¢ will iterate over the catch clausesĀ in the order they were declared looking for a catch clause that matches the type of the error that was thrown:
- Typed catch clauses will match if the <type> declared in the catch clause matches the type of the exception. That means that typed catch clauses can only catch SIL objects thrown using throw. The caught value is available inside the catch block using the defined <varname>
- The catch all clause will catch any exception (SILā¢ Objects or Java exceptions) regardless of type.
If a matching catch clause was found, the instructions within the catch block are executed and the program will continue. If no matching catch clause is found, the exception is thrown inside the outer code block. This makes it possible to nest try-catch blocks within one another and throw exceptions from the inner block to be caught by the topmost one.
Examples
try { number errorCode = 2; throw errorCode; } catch string s { // will not match } catch number err { // will match because a number was thrown //err = 2 }
try { number errorCode = 2; throw errorCode; } catch string s { // will not match } catch { // will match, but the error code is not available in this context }
try { try { throw "Error!"; } catch number errCode { // will not match because "Error!" is a string } } catch string err { // matches // err = "Error!" }Ā
try { runAs("user_that_does_not_exist"); } catch string err { // will not match } catch { // will match }