cancel
Showing results for 
Search instead for 
Did you mean: 

How to catch an error from a ServiceTask?

zlatan316
Champ on-the-rise
Champ on-the-rise
Hello,
I have a Call Activity which contains a Service Task. The service task throws an Exception within the execute method as below:

//Checking if report returns NULL
if(valueString.contains("Nothing")) {
   System.out.println("Null coming through: " + valueString);
   execution.setVariable("valueVar", 0);
   throw new Exception("BPMNNullError");
}
else {
   execution.setVariable("valueVar", valueString);
}


The Service Task in my diagram is using an ErrorBoundaryEvent as below::
http://s28.postimg.org/3yhx51fql/bpmnerror.jpg

However it does not follow the path down to the 'Compensate For Lag' Service Task.

Is there a way I can throw an error in Java, and have it get caught by an ErrorEvent attached to the ServiceTask itself as shown? If not, what is the correct way to create it?

Thanks.
7 REPLIES 7

trademak
Star Contributor
Star Contributor
Yes you can throw a BpmnError in your Java delegate and catch it with an error boundary event.

Best regards,

zlatan316
Champ on-the-rise
Champ on-the-rise
Hello,

The above model however does not work. I throw the error in my execute method, but the flow out of the ErrorBoundaryEvent does not occur. All that happens is my Java code catches the error.

jbarrez
Star Contributor
Star Contributor
If you throw a new BpmnError, and have a boundary event for that error, it will be caught.
Not a generic exception, but a BpmnError.

zlatan316
Champ on-the-rise
Champ on-the-rise
My Eclipse IDE does not recognize 'BPMNException' as a valid type of Exception. However I see I can throw an ActivitiException which is recognize by Eclipse, but unfortunately this also does not let my flow exit from the ErrorBoundaryEvent.

sahusunil
Champ in-the-making
Champ in-the-making
You will need to throw BPMNError (org.activiti.engine.delegate.BpmnError) from your delegate class. Once boundary event will capture this error, it will trigger the next task or step which you have associated with boundary event.

Ah yes, so I can throw a BPMNError after referencing the class correctly. However, it will still not move onto the next step of the flow once the error is thrown.

Here is the relevant XML I am using.
<code>
<serviceTask id="servicetask6" name="Retrieve Non Submission Report" activiti:class="ssrsrunner.RetrieveNonSubmissionReport"></serviceTask>
<boundaryEvent id="boundaryerror1" name="Error" attachedToRef="servicetask6">
   <errorEventDefinition></errorEventDefinition>
</boundaryEvent>
<sequenceFlow id="flow36" sourceRef="boundaryerror1" targetRef="servicetask7">
   <conditionExpression xsi:type="tFormalExpression"><![CDATA[${submissionPercentageVar == -1}]]></conditionExpression>
</sequenceFlow>
</code>

and an excerpt of the updated actual code which throws the BPMNError:
<code>
if(myRunner.submissionStatsMap.get("totalSubmission").get(0).contains("NaN")) {
   System.out.println("Null coming through from Submission %: " + myRunner.submissionStatsMap.get("totalSubmission").get(0));
   execution.setVariable("submissionPercentageVar", -1);
   throw new BpmnError("BPMNNull%Error");
}
</code>

zlatan316
Champ on-the-rise
Champ on-the-rise
I have discovered what was stopping this issue. I had a condition in my flow leading out of the ErrorBoundaryEvent and the Service task it was attached to. If I removed these the error was caught and the correct path was taken. I wonder why having a flow condition, even though they would evaluate correctly, would stop an error from being caught and allow the flow to continue.

I also removed a try catch statement from my code which allowed Activiti full control over the handling of the error thrown.

Thanks for the suggestions all.