You can set an exception handler to customize exception handling in rule conditions and
actions. When you implement the CustomExceptionHandler API, exceptions can either
be ignored or rethrown.
About this task
The default engine behavior is to stop when an exception is thrown. When you implement the
CustomExceptionHandler API, you can choose to enforce the default engine behavior
or to ignore the exception so that the engine can continue to process rules.
To activate the CustomExceptionHandler, you
must implement the interface in the XOM and then specify the implementation class name in Rule
Designer.
Procedure
- Implement the CustomExceptionHandler interface in the XOM. In the
following example, the custom exception handler rethrows exceptions for NumberFormatException:
package com.acme;
import ...
public class MyCustomExceptionHandler implements CustomExceptionHandler {
static Logger logger = Logger.getLogger(MyCustomExceptionHandler.class.getName());
/**
* The custom exception handler must contain a default constructor.
*/
public MyCustomExceptionHandler() {
}
@Override
public void handleConditionException(Exception e) throws Exception {
if (e instanceof NumberFormatException) {
// ignore number format exception
} else {
// rethrow the exception, it will stop the engine execution
throw e;
}
}
@Override
public void handleActionException(Exception e, RuleInstance ruleInstance) throws Exception {
if (e instanceof NumberFormatException) {
// ignore number format exception and log
if (logger.isLoggable(Level.INFO)) {
logger.log(Level.INFO,
"Exception while executing action of rule " + ruleInstance.getRuleName(), e);
}
} else {
// rethrow the exception, it will stop the engine execution
throw e;
}
}
}
- Open Rule
Designer.
- In the Rule Engine panel of the rule project properties window,
enter the custom exception handler name. For example, enter
com.acme.MyCustomExceptionHandler in the Custom exception
handler field.
- Click OK.
Results
The custom exception handler is created when the engine starts, and removed when the engine is
reset.