<p>Mark, Michael, Edson&nbsp;and all,</p>
<div>Thanks very much for participating into this discussion. I am so glad to see that the response&nbsp;has quickly been turned out into an output. Really appreciate your help, Mark. </div>
<div>&nbsp;</div>
<div>Sorry may be a liittle&nbsp;too aggressive, but here comes another thought of mine: if we can have the RHS to have exception handler, why not the LHS has exception handler too? This is sometimes very helpful when someone abuses eval() in LHS.
</div>
<div>&nbsp;</div>
<div>Best Regards,</div>
<div>Yang</div>
<div><br>&nbsp;</div>
<div><span class="gmail_quote">On 8/25/07, <b class="gmail_sendername">Mark Proctor</b> &lt;<a href="mailto:mproctor@codehaus.org">mproctor@codehaus.org</a>&gt; wrote:</span>
<blockquote class="gmail_quote" style="PADDING-LEFT: 1ex; MARGIN: 0px 0px 0px 0.8ex; BORDER-LEFT: #ccc 1px solid">
<div text="#000000" bgcolor="#ffffff">Here is the unit test showing how it works, this uses the programmatic api, you can also use the drools.consequenceExceptionHandler property. This will be in the 4.0.1 release, which is out this monday.
<br><a onclick="return top.js.OpenExtLink(window,event,this)" href="http://jira.jboss.org/jira/browse/JBRULES-1123" target="_blank">http://jira.jboss.org/jira/browse/JBRULES-1123</a><br><br>&nbsp;&nbsp;&nbsp; public void testCustomConsequenceException() throws Exception {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; final PackageBuilder builder = new PackageBuilder();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; builder.addPackageFromDrl( new InputStreamReader( getClass().getResourceAsStream( &quot;test_ConsequenceException.drl&quot; ) ) );<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; final Package pkg = 
builder.getPackage();<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; RuleBaseConfiguration conf = new RuleBaseConfiguration();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CustomConsequenceExceptionHandler handler = new CustomConsequenceExceptionHandler();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; conf.setConsequenceExceptionHandler
( handler );<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; final RuleBase ruleBase = getRuleBase(conf);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ruleBase.addPackage( pkg );<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; final WorkingMemory workingMemory = ruleBase.newStatefulSession();<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; final Cheese brie = new Cheese( &quot;brie&quot;,
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 12 );<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; workingMemory.insert( brie );<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; workingMemory.fireAllRules();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; assertTrue( handler.isCalled() );<br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; public static class CustomConsequenceExceptionHandler implements ConsequenceExceptionHandler {
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private boolean called;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public void handleException(Activation activation,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WorkingMemory workingMemory,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Exception exception) {
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.called = true;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public boolean isCalled() {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return this.called;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; }<br><span class="sg"><br>Mark</span> 
<div><span class="e" id="q_1149a7514ab8743e_2"><br>Mark Proctor wrote: 
<blockquote type="cite">ok I have added a ConsequenceExceptionHandler to RuleBaseConfiguration, default just wraps and re-throws as a runtime exception. You can override this to provide a custom consequence exception handler. But do be aware that if you swallow the working memory integrity may be invalid, if the error happened during a working memory action.
<br><br>Mark<br>Anstis, Michael (M.) wrote: 
<blockquote type="cite">
<div dir="ltr" align="left"><span><font face="Arial" color="#0000ff" size="2">For what it&#39;s worth I think this would be a good idea too.</font></span></div>
<div dir="ltr" align="left"><span></span>&nbsp;</div>
<div dir="ltr" align="left"><span><font face="Arial" color="#0000ff" size="2">Perhaps the default ASM wrapper around the RHS could use a try...catch block and log any exceptions of a rules&#39; activation in a (drools) accessible log? Heck you could even allow certain accepted exceptions to be defined as a property of the rule; and any other non-defined exception types cause the session to become invalidated. If we&#39;re ever to let &quot;the business&quot; define rules we need to accept they might make mistakes that we&#39;re better off capturing and report back than invalidate the whole session. 
</font></span><span><font face="Arial" color="#0000ff" size="2">Why should a whole session be invalidated because a single rule activation failed anyway?</font></span></div>
<div dir="ltr" align="left"><span></span>&nbsp;</div>
<div dir="ltr" align="left"><span><font face="Arial" color="#0000ff" size="2">With kind regards,</font></span></div>
<div dir="ltr" align="left"><span></span>&nbsp;</div>
<div dir="ltr" align="left"><span><font face="Arial" color="#0000ff" size="2">Mike</font>&nbsp;</span></div><br>
<blockquote style="MARGIN-RIGHT: 0px">
<div lang="en-us" dir="ltr" align="left">
<hr>
<font face="Tahoma" size="2"><b>From:</b> <a onclick="return top.js.OpenExtLink(window,event,this)" href="mailto:rules-users-bounces@lists.jboss.org" target="_blank">rules-users-bounces@lists.jboss.org</a> [<a onclick="return top.js.OpenExtLink(window,event,this)" href="mailto:rules-users-bounces@lists.jboss.org" target="_blank">
mailto:rules-users-bounces@lists.jboss.org</a>] <b>On Behalf Of </b>Yang Song<br><b>Sent:</b> 24 August 2007 16:16<br><b>To:</b> Rules Users List<br><b>Subject:</b> Re: [rules-users] How to catch Exceptions when firing rules
<br></font><br>&nbsp;</div>
<div>Thanks a lot for the answer, Mark. But I don&#39;t think it makes sense. </div>
<div>&nbsp;</div>
<div>Because in some scenarios, you cannot guarantee the consequence part of rule is 100% correct -- there could be errors happening in run-time which are hard to predict, especially when a complex action or logic will be executed as the concequece. 
</div>
<div>&nbsp;</div>
<div>There should at least be some mechanisms to tell whoever fires the rule that there is something wrong during the rule firing process, then and he&nbsp;can&nbsp;do something, e.g. create a new session. Also it should enable the&nbsp;rule firer&nbsp;to catch these exceptions and do the clean up work silently -- instead of leaving these things on the stderr even cannot be seen in the logs. This will make the program depending on the JBoss Rules to be more robust. 
</div>
<div>&nbsp;</div>
<div>What do you think? If JBoss Rules already has the ability to do this job, can you please let me know?</div>
<div>&nbsp;</div>
<div>Thanks again,</div>
<div>Yang<br><br>&nbsp;</div>
<div><span class="gmail_quote">On 8/24/07, <b class="gmail_sendername">Mark Proctor</b> &lt;<a onclick="return top.js.OpenExtLink(window,event,this)" href="mailto:mproctor@codehaus.org" target="_blank">mproctor@codehaus.org
</a>&gt; wrote:</span> 
<blockquote class="gmail_quote" style="PADDING-LEFT: 1ex; MARGIN: 0px 0px 0px 0.8ex; BORDER-LEFT: rgb(204,204,204) 1px solid">
<div text="#000000" bgcolor="#ffffff">Once an exception is thrown on a conseuqence the current session is considered invalid. You&#39;ll need to add the try catch inside of the actual consequence.<br><br>Mark<br>Yang Song wrote: 
<blockquote type="cite">
<div><span>
<div>Hi, </div>
<div>&nbsp;</div>
<div>Anyone knows how to catch the exception when firing the rules? </div>
<div>&nbsp;</div>
<div>I wrapped the session.fireAllRules() method using try...catch, however it doesn&#39;t work: when someone wrote&nbsp;bad code in the rule&#39;s action part, the Exception will be thrown and printed to the stderr, and this will make the rule engine stop working --&nbsp;the try...catch outside doesn&#39;t help anything. 
</div>
<div>&nbsp;</div>
<div>If&nbsp;&nbsp;the exception&nbsp;thrown from&nbsp;the rule&#39;s action part&nbsp;can be caught externally,&nbsp;the system can be protected from interrupting Exception.</div>
<div>
<p><font style="BACKGROUND-COLOR: rgb(255,255,255)" face="courier new,monospace" color="#3333ff">try {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _log.debug(&quot;Firing rules in : &quot; + getName());</font></p>
<p><font style="BACKGROUND-COLOR: rgb(255,255,255)" face="courier new,monospace" color="#3333ff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; session.fireAllRules();</font></p>
<p><font style="BACKGROUND-COLOR: rgb(255,255,255)" face="courier new,monospace" color="#3333ff">} catch (Exception e) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _log.info(&quot;Error when firing rules: &quot;, e);<br>}</font> <br></p>
<p>Thanks, <br>Yang</p></div></span></div><pre><hr width="90%" size="4">
_______________________________________________
rules-users mailing list
<a onclick="return top.js.OpenExtLink(window,event,this)" href="mailto:rules-users@lists.jboss.org" target="_blank">rules-users@lists.jboss.org</a>
<a onclick="return top.js.OpenExtLink(window,event,this)" href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a>
  </pre></blockquote><br>&nbsp;</div><br>_______________________________________________<br>rules-users mailing list<br><a onclick="return top.js.OpenExtLink(window,event,this)" href="mailto:rules-users@lists.jboss.org" target="_blank">
rules-users@lists.jboss.org </a><br><a onclick="return top.js.OpenExtLink(window,event,this)" href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users
</a><br><br></blockquote></div><br></blockquote><pre><hr width="90%" size="4">
_______________________________________________
rules-users mailing list
<a onclick="return top.js.OpenExtLink(window,event,this)" href="mailto:rules-users@lists.jboss.org" target="_blank">rules-users@lists.jboss.org</a>
<a onclick="return top.js.OpenExtLink(window,event,this)" href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a>
  </pre></blockquote><br><pre><hr width="90%" size="4">
_______________________________________________
rules-users mailing list
<a onclick="return top.js.OpenExtLink(window,event,this)" href="mailto:rules-users@lists.jboss.org" target="_blank">rules-users@lists.jboss.org</a>
<a onclick="return top.js.OpenExtLink(window,event,this)" href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a>
  </pre></blockquote><br></span></div></div><br>_______________________________________________<br>rules-users mailing list<br><a onclick="return top.js.OpenExtLink(window,event,this)" href="mailto:rules-users@lists.jboss.org">
rules-users@lists.jboss.org</a><br><a onclick="return top.js.OpenExtLink(window,event,this)" href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users
</a><br><br></blockquote></div><br>