How to catch and handle exceptions at individual rule level in Drools?
Aim is that exceptions at a single rule should not impact the execution of the rest of the rules.
I know that we can use try catch in RHS, but can we have a control at much higher level irrespective of what LHS or RHS is.
Something like:
fireAllRules( new DefaultAgendaEventListener() {
@Override
public void whenExceptionAtRule(Exception exception) {
//handle exception when
}
})
First, you need to implement the
org.kie.api.runtime.rule.ConsequenceExceptionHandler
interface:Then, it all depends on how are you creating your
KieBases
. If you are doing it manually (without using a kmodule.xml file), then you need to create aKieBaseConfiguration
to specify what class you want to use to handle exceptions:And then use this
kconfig
object when creating yourKieBase
:I couldn't find a declarative way to register the handler in the kmodule.xml file though.
Hope it helps,