cancel
Showing results for 
Search instead for 
Did you mean: 

How to do exception handling in Activiti v6.0.0

nami
Champ in-the-making
Champ in-the-making

Hi I am new to Activiti Framework, Can someone please give information on how can we do exception handling in activiti v6.0.0 . Please attach a sample bpmn xml file with exception as a service task, and also the exception class(java code) if possible.

I found this while searching for info on exception handling, but this code doesn't work for v6.0.0. It only works for v5.19.0.

Exception Class:

import org.activiti.engine.impl.pvm.PvmTransition;
import org.activiti.engine.impl.pvm.delegate.ActivityBehavior;
import org.activiti.engine.impl.pvm.delegate.ActivityExecution;


public class ThrowsExceptionBehavior implements ActivityBehavior {

private static final long serialVersionUID = 1L;

private org.activiti.engine.impl.el.FixedValue text;

private org.activiti.engine.impl.el.Expression exp;

public void execute(ActivityExecution execution) throws Exception {
//String text = (String) execution.getVariable("text");

PvmTransition transition = null;
try {
System.out.println("In ThrowsExceptionBehavior class: "+ text);
//throw new Exception("Activiti Exception");
transition = execution.getActivity().findOutgoingTransition("no-exception");
} catch (Exception e) {
transition = execution.getActivity().findOutgoingTransition("exception");
}
execution.take(transition);
}
}

BPMN File :

<startEvent id="startevent1" name="Start"></startEvent>
<sequenceFlow id="flow1" sourceRef="startevent1" targetRef="servicetaskCustom"></sequenceFlow>
<serviceTask id="servicetaskCustom" name="test" activiti:class = "@ThrowsExceptionBehavior" >
<extensionElements>
<activiti:field name="text" stringValue="Hello World" />
</extensionElements>
</serviceTask>
<sequenceFlow id="no-exception" sourceRef="servicetaskCustom" targetRef="endevent1" />
<sequenceFlow id="exception" sourceRef="servicetaskCustom" targetRef="fixException" />

<serviceTask id="fixException" name="print" activiti:expression="#{result.print()}"></serviceTask>
<sequenceFlow id="exceptionOccurred" sourceRef="fixException" targetRef="endevent1" />

8 REPLIES 8

hari
Star Contributor
Star Contributor

Hi, 

The way in which I do is, I define a condition on the outgoing transitions. One will have something like ${Path=="Success"} and other will have something like ${Path=="HandleException"}. 

My service task will have code something as below. 

try {
      System.out.println("In Service Task");

      // Do something 
      execution.setVariable("Path","Success")
} catch (Exception e) {
      execution.setVariable("Path","HandleException")
}

salaboy
Star Collaborator
Star Collaborator

I would also suggest to really think about if you want to handle an exception in the process model or if it is a technical matter. If it is a technical matter and not an exceptional business path you should deal with it in Java behind the scenes.

nami
Champ in-the-making
Champ in-the-making

i want to handle some business exceptions as well. 

salaboy
Star Collaborator
Star Collaborator

Business Exceptions are fine.. just make sure that those are not Java Exceptions. 

nami
Champ in-the-making
Champ in-the-making

but i need more clarity on how to do this in activiti 6.0.0. Can you please provide more details on this ?

nami
Champ in-the-making
Champ in-the-making

can u tell me are you using exclusive gateways to manage your conditions in the bpmn xml file ?

nami
Champ in-the-making
Champ in-the-making

I am using this as exclusive gateway in my bpmn xml file. 

<exclusiveGateway id="exceptionOccured" name="exceptionOccured"/>
<sequenceFlow id="exception" sourceRef="exceptionOccured"
targetRef="catchException">
<conditionExpression xsi:type="tFormalExpression"><![CDATA[${Path == "HandleException"}]]>
</conditionExpression>
</sequenceFlow>
<sequenceFlow id="no-exception" sourceRef="exceptionOccured"
targetRef="endevent1">
<conditionExpression xsi:type="tFormalExpression">${Path != "HandleException"}
</conditionExpression>
</sequenceFlow>

But it throws below exception :

org.activiti.engine.ActivitiException: Unknown property used in expression: ${Path == "HandleException"}
at org.activiti.engine.impl.el.JuelExpression.getValue(JuelExpression.java:52) ~[activiti-engine-6.0.0.jar:6.0.0]

I am setting the Path Variable in the catch block of my java task code

try{

   //do something

}

catch (Exception e) {
      logger.error("rest call failed " + e.getMessage() +e.getMessage());
      new ThrowsExceptionBehavior() {
            private static final long serialVersionUID = 1L;
            @Override
            public void execute(DelegateExecution execution) {
            execution.setVariable("Path", "HandleException");
            }
      };
}

hari
Star Contributor
Star Contributor

Hi, 

You should atleast keep the variable available in your process by the time it reaches your exclusive gateway. The problem above is that you are setting the variable Path a value only when the you come across an exception. This means that when there is no exception, variable Path is unknown. That was the reason for your exception. 

If you look at how I did this, irrespective of whether I see an exception or not, I am setting the value for variable Path. 

try {
      System.out.println("In Service Task");

      // Do something 
      execution.setVariable("Path","Success")
} catch (Exception e) {
      execution.setVariable("Path","HandleException")
}