What is Drools exception handling

让我们描述了Drools的异常处理的细节. 在Drools中, 默认的异常处理程序被称为 “后果异常处理程序”. 它提供了类似下面的最少信息.

一) The rule
b) Where the exception took place
Ç) The cause with java stack trace

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

ConsequenceException(Throwable rootCause, Rule rule)

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

“ “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);

如果 (kbuilder.hasErrors()) {
System.out.println(kbuilder.getErrors().的toString());
throw new RuntimeException(“Unable to compile ”Test.drl”.”);
}

============================================= ============================================== 在亚马逊上购买最佳技术书籍,en,电工CT Chestnutelectric,en
============================================== ---------------------------------------------------------------- electrician ct chestnutelectric
error

Enjoy this blog? Please spread the word :)

Follow by Email
LinkedIn
LinkedIn
Share