I had a rule which crashed because of an not existing command to an item. This exception causes the rule to not run to the end.
Is there a way to prevent this, so that the rules runs to the end
Example:
rule "PresistenceWiederherstellen"
when
Item SW_PresistenceWiederherstellen changed to ON
then
gRestoreState.members.forEach[item |
item.sendCommand(item.previousState(false,“mapdb”).state.toString)
]
SW_PresistenceWiederherstellen.postUpdate(OFF)
end
This rule crashed on contacts, because contacts cannot handle sendCommand (my fault as a programmer). But this causes the rule to stop and not do SW_PresistenceWiederherstellen.postUpdate(OFF).
Any idea to solve this? and let the rule continue with next line.
I don’t think so. If it is broken, you have to fix it.
In many cases it would make no sense to continue operations with possible “faulty” variables etc. That would be harder for us to detect, let alone fix.
You have to add try/catch/finally to your rules or add error checking to prevent the exception from being thrown in the first place.
The entire purpose of exceptions is to stop everything and back out of the programming stack until you find a catch that handles that exception. The only way to prevent that is to prevent the exception in the first place or provide your own catch.
then
try {
// do some stuff
}
catch(Throwable t) {
logError("Error", "Some bad stuff happened in my rule: " + T.toString)
}
finally {
// always runs even if there was an error, good place for cleanup
}