Handling activity exceptions and workflow termination

Hi all,

I have the following scenario:

try {
   activity1.function1();
} catch (SomeCheckedException e) {
   log.info("Exception thrown from activity 1");
  // Terminate the workflow
}
activity2.function2();

There are two activities. Suppose some checked exception, “SomeCheckedException” is thrown from activity1, I want to catch this in workflow and do some custom logic in the catch block. I tried throwing two exceptions from activity1:
a) SomeCheckedException1, which is not set in the activity retry options setDoNotRetry
b) SomeCheckedException2, which is set in the activity retry options setDoNotRetry
In either of the cases, the execution is not reaching the catch block.

  1. What can be done so that I can execute some custom logic in the catch block?
  2. What is the proper way of terminating the workflow, so that activity2.function2() is not invoked in case activity1.function1() throws some exception?
  3. Is there any recommended way of throwing exceptions from activities?

Thanks,
Joe

Hi Joe,

a) SomeCheckedException1, which is not set in the activity retry options setDoNotRetry

Activity executions will retry based on the retry options, if you don’t limit the number of retries or set schedule_to_close, the activity will retry indefinitely if this exception is thrown (while the workflow is in the open state.)

b) SomeCheckedException2, which is set in the activity retry options setDoNotRetry

If the activity fails, with a non-retryable failure, it will throw an ActivityFailure which is what you want to catch. See this example. You can get the initial error with ((ApplicationFailure) af.getCause()).getType())

  1. What can be done so that I can execute some custom logic in the catch block

I think I answered this above; let me know if you have questions.

  1. What is the proper way of terminating the workflow, so that activity2.function2() is not invoked in case activity1.function1() throws some exception?

one way, in your catch block you can rethrow the exception, or don’t catch it if you don’t have to run any extra step after function1 fails.

  1. Is there any recommended way of throwing exceptions from activities?

I guess it depends on the use-case.
If you want to throw a non-retryable exception, you can do what you are already doing (set retryOptions.setDoNotRetry ) or throw ApplicationFailure.newNonRetryableFailure(...) from your activity code.

Let me know if it helps,
Antonio

1 Like

Never use checked exceptions in the activity and workflow signatures.

1 Like

Hi Antonio,

Thanks for the detailed explanation!
Throwing the exception using Activity.wrap and catching ActivityFailure resolved my issue.

Thanks,
Joe

Sure, noted.