ILOG JRules User Guide > Writing Rules > Concepts > Technical Rules > Technical Rule Actions > Exception Handling Statements

IRL provides mechanisms for reporting and handling exceptions. When an error occurs, the rule throws an exception. This means that the normal flow of the rule is interrupted, and the rule engine attempts to find an exception handler, that is, a block of code that handles a particular type of error. The exception handler generally performs the necessary actions to recover from the error.

Exception Setup

Before you can catch an exception, code somewhere must throw one. Any Java code can throw an exception.

A throw expression is any kind of expression whose type is assignable to the Java Throwable type or subclass. A throw expression can be specified either in the API or in a rule. When an exception is thrown it can be caught by the API or a rule--with a try-catch-finally statement or by any Java code--which causes the IRL code to execute.

Note
A thrown exception is an IlrUserRuntimeException subtype of IlrRunTimeException which encapsulates the exception thrown by the throw statement.

The Try-Catch-Finally Statements

Here is the general form of these statements:

try {
    statements
} 
catch (ExceptionType1 name) {
    statements
} 
catch (ExceptionType2 name) {
    statements
} 
...
finally {
    statements
}

IRL allows general exception handlers that handle multiple types of exceptions. However, it is recommended that exception handlers be more specialized in order to determine what type of exception occurred and assist in the recovery of these errors. Handlers that are too general can make code more error prone by catching and handling exceptions that were not anticipated and for which the handler was not intended.

The following try statements demonstrate how to create an exception and throw it.

then {
   try {
      method1(1) ; 
      System.out.println("Call method1(1) was OK") ; 
   }
   catch ( Exception e ) { 
   System.out.println("Catch from method1(1) call") ; 
   }
   try {
      method1(2) ;
      System.out.println("Call method1(2) was OK") ;
   }
   catch ( Exception e ) { 
      System.out.println("Catch from method1(2) call") ; 
      System.out.println("Exception details :-") ;
      System.out.println("Message: " + e) ; 
   }
}

The try-catch statements use a method call from a Java class to throw an exception when the variable passed into method1 is not equal to 1. Here is method1:

public static void method1(i) throws Exception{
   if (i == 1) { 
      System.out.println("method1 - Things are fine \n") ; 
   }
   else {        
      System.out.println("method1 - Somethings wrong! \n") ;
      throw new Exception("method1 - Its an exception! \n") ; 
   } 
}

Exceptions thrown by any rule or Java code within the scope of the rule engine will be caught by the try-catch statements for that specific exception.

The following CatchCustomer rule provides an example of a try-catch statement block capable of catching exceptions thrown by the ILOG Rule language and/or Java code. The rule uses two classes, Customer and Item, and two subclasses of the Java Exception class, TooHighExpense and IsNotMember.

rule CatchCustomer
{
  when {
   ?c: Customer();
   ?item: Item();
  }
  then {
    try {  
      System.out.println("Customer " + ?c.getName() + " wants to buy "
                         + " item " + ?item.getLabel() + " for a price of " +
                         ?item.getPrice() + "$");
      IsMember(?c);
      ?c.buy(?item);
    }
    catch (TooHighExpense ex) {
      System.out.println("M/Mrs/Mz " + ?c.getName() + " you have already
                         bought:");
      int j = 0;
      for(j = 0; j<?c.getItems().size(); j++) {
        java.util.Enumeration ?i = ?c.getItem(j);
        System.out.println("   Item: " + ?i.getLabel() + " costs " +
                           ?i.getPrice() + "$");
      }
      System.out.println("You have at your disposal " + ?c.getBudget() + "$");
      System.out.println("The current purchase is not allowed");
    }
    catch (IsNotMember ex) {
      System.out.println("M/Mrs/Mz " + ?c.getName() + 
                         ", you are not a member; would you like to
                          subscribe?");
    }
  }
}

In the example, a customer is given a budget. The customer is not allowed to buy items that will exceed the allocated budget. If the customer attempts to buy items that exceed the budget, a TooHighExpense exception is thrown. In addition, to buy items the customer must be a member. If a nonmember attempts to buy an item, an IsNotMember exception is thrown.

The two Java classes IsNotMember and TooHighExpense used in the CatchCustomer rule are shown here:

public class IsNotMember extends Exception
{
    public IsNotMember(String name){
      super();
      this.customer = name;
    }
    public void printStackTrace(PrintWriter s){ 
      s.println("An IsNotMember exception has been caused by customer "
                + customer + ". We are sorry but you can not make a purchase "
                + "without being a member.");
      super.printStackTrace(s);
      }
    String customer;
};
 
 
public class TooHighExpense extends Exception
{
    public TooHighExpense(String name, int expense, int limit, int price){
      super();
      this.expense = expense;
      this.limit = limit;
      this.price = price;
      this.customer = name;
    }
    public void printStackTrace(PrintWriter s){ 
      s.println("A TooHighExpense exception has been caused by customer "
                + customer + ". His current expense is " + expense + 
                ", his authorized budget is " + limit +
                ". The purchase of the item whose price is " + price
                + " is not allowed.");
      super.printStackTrace(s);
      }
    private int price;
    private int limit;
    private int expense;
    String customer;
};

Related Concepts

Technical Rule Actions
Loops
Branching Statements

Related Tasks

Working with Technical Rules
Refactoring Rule Artifacts

Related Reference

ILOG Rule Language