Handle activity failure exceptions in workflows

Hi Team,
I have a workflow with 100s of activities,some sequential and some parallel.
How to I catch exception in such a scenario?
Do I need to put try/catch in each sequential activity?
can you please tell me if there is any better way to handle it?

Also, in case of parallel activites, I want to continue my workflow if some activities fail.How do I handle exception in parallel activity case?
Thanks in advance.

1 Like

Workflow code handles exceptions exactly the same way as any other Java code. You handle exceptions according to your business logic and can use various programming techniques to avoid code duplication.

In the majority of cases intermittent activity errors are handled by automatic retries according to a retry options passed to the activity. So workflow code doesn’t need to handle them at all.

If you want to continue workflow execution on non intermittent activity failure then try-catch is the right approach.

If you need to surround a lot of activities with try-catch use the standard Java programming techniques to reduce the code duplication. For example:

  public static <T> T ignoreException(Supplier<T> function, T defaultValue) {
    try {
      return function.get();
    } catch (Exception e) {
      return defaultValue;
    }
  }

Can be used as

String result = ignoreException(() -> activities.activity1(arg), "Whatever");

If you want to get really fancy then you can create your own dynamic interceptor which uses your custom annotations to mark activities that have to ignore their exceptions.

1 Like

Ok, this is clear but there is something I don’t understand: in Java you always have to catch exceptions at some level or declare that they are thrown.

So, suppose I have a workflow method that calls an activity method where an exception can be thrown (e.g., my code tries to connect to a service to retrieve some info or do something but fails).

I cannot avoid catching the exception, otherwise I would need to declare the activity method “throws Exception” and catch it in the workflow method. So in the end it seems to me I have to manage the retries myself, which is not what I would expect. Is there any way to make the activity fail so that it is temporal itself that handles retries?

Maybe I’m completely misunderstanding temporal’s concepts…

OK, maybe it’s done by rethrowing the exception wrapped by Activity.wrap. Is this correct?

try{
...
}catch(Exception e)
{
    throw Activity.wrap(e);
}

I am feeling so lame, sorry guys…

Activity methods should not use throw checked exceptions. As you correctly pointed out if there is a need to propagate a checked exception use throw Activity.wrap(e).