Examples

Exception handling syntax for the rule language grammar is presented through some examples.

try/finally Example

String localStatus = "default status";
try {
    //readStatus might throw an exception
    localStatus = readStatus();
} finally {
    //If readStatus() throws an exception,     //MyScorecard.status will be set to "default status"
    //but the exception won't be caught here.
    //Otherwise MyScorecard.status will be set to the
    //return value of readStatus()
    MyScorecard.status = localStatus;
}

try/catch/finally Example

String localStatus = "default status";
try {
    //readStatus might throw an exception
    localStatus = readStatus();
} catch(Exception exp) {
    System.debugOut("readStatus() threw an exception with message"
                     + exp@message);
} finally {
    //If readStatus throws an exception,
    //MyScorecard.status will be set to "default status"
    //Otherwise MyScorecard.status will be set to the
    //return value of readStatus()
    MyScorecard.status = localStatus;
}

try/catch Example

String localStatus = "default status";
try {
    //readStatus might throw an exception
    localStatus = readStatus();
} catch(Exception exp) {
    System.debugOut("readStatus() threw an exception with message "
             + exp@message);
}
    //If readStatus throws an exception,
    //MyScorecard.status will be set to "default status"
    //Otherwise MyScorecard.status will be set to the
    //return value of readStatus()
MyScorecard.status = localStatus;

try/catch Example with Checking errorType and re-throw

String localStatus = "default status";
try {
    //readStatus might throw an exception
    localStatus = readStatus();
catch(Exception exp) {   
    if (exp@errorType == "java.lang.NullPointerException")   
         throw exp;  
    System.debugOut("readStatus() threw an exception with message " + exp@message); 
} 
//If readStatus throws an exception other than NullPpointerException, 
//MyScorecard.status will be set to "default status"
//If readStatus throws NPE, MyScorecard.status will be set to "default status" and NPE will be re-thrown.
//Otherwise MyScorecard.status will be set to the return value of readStatus()
MyScorecard.status = localStatus;