What is Drools exception handling

Let us describe Drools exception handling in details. In Drools, the default exception handler is known as “Consequence Exception Handler”. It provides minimum information like the following.

a) The rule
b) Where the exception took place
c) The cause with java stack trace

Drools also allow to write your own custom exception handler.The “ConsequenceException” extends “java.lang.RuntimeException”.The constructor signature is given below.

ConsequenceException(Throwable rootCause, Rule rule)

The only method available is “public Rule getRule()”.

The “ConsequenceExceptionHandler” is an interface which is implemented to write custom handler.The only method available is given below and it is overwritten to do custom handling.

void handleException(Activation activation,
WorkingMemory workingMemory,
Exception exception)

Sample code is given below :-

import java.io.Externalizable;
import org.drools.runtime.rule.ConsequenceExceptionHandler;
public class CustomConsequenceExceptionHandler
implements ConsequenceExceptionHandler, Externalizable {
public void handleException( Activation activation,
WorkingMemory workingMemory,
Exception exception ){
// Write custom handling code here.

To improve custom handling “RuntimeException” can also be extended.

For identifying rule file (.drl) compilation error, “hasErrors()” method of “KnowledgeBuilder” class can be used. Following is a sample code snippet for implementing the method.

KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newFileResource("D:/Workspace/TestProj/src/Test.drl"), ResourceType.DRL);

if (kbuilder.hasErrors()) {
throw new RuntimeException(“Unable to compile \”Test.drl\”.”);

Please follow and like us:

Leave a Reply

Your email address will not be published. Required fields are marked *

× 8 = 56

============================================= ============================================== Buy TechAlpine Books on Amazon
============================================== ----------------------------------------------------------------

Enjoy this blog? Please spread the word :)

Follow by Email