Exception Rule .. How does it work?

  • 31 August 2012
  • 2 replies
  • 3 views

Badge +8

Greetings, (blackpearl 4.6.1)


I understand that when I check the box for the Exception Rule, that when an error occurs, the code that I put in will run..


What I don't understand is what happens next...


If I enable the rule on the Event Wizard, does it skip that event? How can I work it so that I fix the issue with that event and re-try the event?


Similar question at the activity level.. how do I restart the entire activity if an error occurs within it?


Also.. how do I parse the error information to determine what it is that happened? I know the error I can fix, but others I need to stop the process.


Is there an error handling document that goes into these details? Or can someone provide some instruction here?


Thanks.


2 replies

Badge +8

The important option that you should explore here is the "Log exception to the server". This allows you to choose whether or not the process will go into an error state or not. If there is an error and the flag is not checked, the event will be skipped and the next event will execute (or the activity completes if there are no events left).


 


If you add the exception handler on Process level, you can have a look at the following article by Eugene, he uses the exception rule to send an intelligent email:


http://www.k2underground.com/forums/t/11716.aspx


While this is not exactly what you want, it will give you insight into what options you have, especially with regards to the object that raised the exception (see the "switch (K2.ContextType)" portion) and the exception itself.


 


Having said all that, I would however recommend that you do not try to repair errors with loops. If not done properly, you can potentially run into an infinite loop that can bring your whole server down.

Badge +8

I opened a ticket to get some details.. Here they are for the rest of the community.




-----


Hi Doug,

Regarding your questions:

1. If I enable the rule on the Event Wizard, does it skip that event?

If you configure the Exception Rule to "Log exception to the server", regardless of where the exception is trapped (Process, Activity or Event level) the process instance will go into "Error" status where the error occurred. In this case the Event.

In other words, if an exception occurs in an Event and the Event does not have an Exception Rule configured it will bubble up to the Activity Exception Rule and get handled there. If there is no Activity Exception Rule configured it will bubble up to the Process Exception Rule and get handled there. If there is no Process Exception Rule configure, the process instance will go into Error status at the Event where the exception occurred.

2. How can I work it so that I fix the issue with that event and re-try the event?

The Event does not get retried. The exception handler just allows you to trap the exception, take some action, then either ignore the exception and allow the process to continue or put the process into Error status where the exception occurred.

It may be possible to call GotoActivity to reinstantiate the Activity in which the exception occurred depending on where the exception occurred. For example, if the exception occurred in a Server Event you could get the ServerEventContext object then call GotoActivity since this method is available on the Server Event. See the Best Practices document referenced below. There is a code example that should make this more clear.

3. Similar question at the activity level.. how do I restart the entire activity if an error occurs within it?

See question #2.

4. Also.. how do I parse the error information to determine what it is that happened? I know the error I can fix, but others I need to stop the process.

You can interrogate the K2.ExceptionObject in the Exception Rule code. See the Best Practices document referenced below in question #5.

5. Is there an error handling document that goes into these details? Or can someone provide some instruction here?

There is K2 blackpearl Best Practices document that discusses Exception Handling and includes a good code example of how to interrogate the ExceptionObject: http://help.k2.com/en/KB000352.aspx


-----


Hmmm...
So here's my issue. I have a Word Document event that is going to insert into a content control.. SharePoint is giving me fits at the moment because there is the possibility that the document will be "locked for shared use" for an hour. So when K2 tries to do the insert, the error is thrown and process stops. I am very close to a code solution that will release that lock (because I know that the document will not be open by anyone at this point in the process).

So what I want is to check the error to see if it's this kind, perform my code to release the document and then re-try the Word Document event... (if it's not that kind of error, then stop the process as it would normally error)

Are you saying that my only option is to re-run the entire activity with the GoTo activity option.?
Will I have that in the Word event? Or is it only in the Server event?
I have two word events in the same activity that operate on the same document.. I think if I error on the first one and clear it, then both will run successfully. I don't think there is a possibility where I'd error out only on the second one and accidentally run the first one again after the error handling restarted the activity. What do you think?
Or.. should I just catch the error, check to see if it's the one I want to catch.. if so, set a data field, wait an hour (can I wait an hour in code?). Then put a line rule in place that checks for my data field and loops back on the activity if it's true. If not, the other line is followed.???

Seams like it should be a feature to re-run the event after the condition is cleared.... feature request?
   --Added by Doug Gilmour on 7 September 2012, 14:51 GMT 00:00.

-----


Hi Doug,

As you suggest, you should be able to check the exception in the Exception Rule code to determine if it's the error in question then run your code to release the lock on the document, but the Event is not going to execute again. Even if you don't put the process instance into Error status the event will not get executed again - the process will continue executing immediately after the point where the exception occurred.

That said, the only option I can think of is to re-instantiate the activity so the Word Documents Event gets executed again. The ContextObject for a Word Documents Event is "ServerEventConext" so will have access to the GotoActivity method. This would likely be cleaner that keeping track of variables and using a line rule that loops back to the same activity, but either solution should work.

A few things to note:
1. GotoActivity has two signatures, one with an "expireAll" parameter. By default, GotoActivity will expire ALL active activities and goto to the specified activity. If your process has parallel paths where multiple activities are active at the same time, make sure to call GotoActivity with the "expireAll" parameter set to false.

2. Be careful to ensure the exception gets handled properly - if you call GotoActivity and the same event continues to fail, the process could get stuck in a loop (and will be stuck in "Running" status) which potentially could cause performance or other issues.

I also suggest splitting the 2 Word Documents Events into separate activities if there is any chance that running the first event twice is going to cause any problems. If the first event succeeds and the second event throws an exception, when you do a GotoActivity back to the same activity the first event will run again.

I agree it would be nice to have some sort of flag that would allow you to "re-execute" the code that caused the exception, but this currently does not exist. I will log this as a feature request.


Reply