# Throwing Exception vs Failure in workflow

**URL:** <https://community.temporal.io/t/throwing-exception-vs-failure-in-workflow/1059>\
**Category:** Community Support\
**Tags:** exception\
**Created:** [November 21, 2020, 2:18pm UTC](https://community.temporal.io/t/throwing-exception-vs-failure-in-workflow/1059 "2020-11-21T14:18:45Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![sdwr98](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/sdwr98/32/423_2.png) [@sdwr98](https://community.temporal.io/u/sdwr98)\
**Post date:** [November 21, 2020, 2:18pm UTC](https://community.temporal.io/t/throwing-exception-vs-failure-in-workflow/1059/1 "2020-11-21T14:18:45Z")

</div>

Hi all,

I have a question about the Failure hierarchy and what the best practice is for handling workflow errors. Are workflows supposed to throw only Failures, or is it acceptable to throw an exception (e.g. IllegalArgumentException) from workflow code?

I ask because in the unit tests, throwing a Failure seems to work fine but throwing an exception seems to put the unit test into a loop of retries with the below message. This example is from WorkflowTestingTest.testFailure(), altered to throw IllegalArgumentException instead of ApplicationFailure. The below message gets printed out repeatedly every few seconds

Thanks!  
Scott

```
	09:15:43.431 [Workflow Executor taskQueue="test-workflow", namespace="default": 3] ERROR i.t.internal.worker.PollerOptions - uncaught exception
java.lang.RuntimeException: Failure processing workflow task. WorkflowId=c5ac197d-598b-4e8e-98f3-9aa6019380cd, RunId=d8585e55-106d-43fe-b5d4-fc436992f71f
	at io.temporal.internal.worker.WorkflowWorker$TaskHandlerImpl.wrapFailure(WorkflowWorker.java:337)
	at io.temporal.internal.worker.WorkflowWorker$TaskHandlerImpl.wrapFailure(WorkflowWorker.java:275)
	at io.temporal.internal.worker.PollTaskExecutor.lambda$process$0(PollTaskExecutor.java:79)
	at io.temporal.internal.worker.PollTaskExecutor$$Lambda$76/0000000000000000.run(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:823)
Caused by: io.temporal.internal.replay.InternalWorkflowTaskException: Failure handling event 17 of 'EVENT_TYPE_WORKFLOW_TASK_STARTED' type. IsReplaying=false, PreviousStartedEventId=17, workflowTaskStartedEventId=17, Currently Processing StartedEventId=17
	at io.temporal.internal.statemachines.WorkflowStateMachines.handleEvent(WorkflowStateMachines.java:193)
	at io.temporal.internal.replay.ReplayWorkflowRunTaskHandler.handleEvent(ReplayWorkflowRunTaskHandler.java:140)
	at io.temporal.internal.replay.ReplayWorkflowRunTaskHandler.handleWorkflowTaskImpl(ReplayWorkflowRunTaskHandler.java:180)
	at io.temporal.internal.replay.ReplayWorkflowRunTaskHandler.handleWorkflowTask(ReplayWorkflowRunTaskHandler.java:150)
	at io.temporal.internal.replay.ReplayWorkflowTaskHandler.handleWorkflowTaskWithEmbeddedQuery(ReplayWorkflowTaskHandler.java:199)
	at io.temporal.internal.replay.ReplayWorkflowTaskHandler.handleWorkflowTask(ReplayWorkflowTaskHandler.java:114)
	at io.temporal.internal.worker.WorkflowWorker$TaskHandlerImpl.handle(WorkflowWorker.java:309)
	at io.temporal.internal.worker.WorkflowWorker$TaskHandlerImpl.handle(WorkflowWorker.java:275)
	at io.temporal.internal.worker.PollTaskExecutor.lambda$process$0(PollTaskExecutor.java:73)
	... 4 common frames omitted
Caused by: java.lang.RuntimeException: WorkflowTask: failure executing SCHEDULED->WORKFLOW_TASK_STARTED, transition history is [CREATED->WORKFLOW_TASK_SCHEDULED]
	at io.temporal.internal.statemachines.StateMachine.executeTransition(StateMachine.java:140)
	at io.temporal.internal.statemachines.StateMachine.handleHistoryEvent(StateMachine.java:91)
	at io.temporal.internal.statemachines.EntityStateMachineBase.handleEvent(EntityStateMachineBase.java:63)
	at io.temporal.internal.statemachines.WorkflowStateMachines.handleEventImpl(WorkflowStateMachines.java:210)
	at io.temporal.internal.statemachines.WorkflowStateMachines.handleEvent(WorkflowStateMachines.java:178)
	... 12 common frames omitted
Caused by: java.lang.IllegalArgumentException: test
	at io.temporal.internal.testing.WorkflowTestingTest$FailingWorkflowImpl.workflow1(WorkflowTestingTest.java:135)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at io.temporal.internal.sync.POJOWorkflowImplementationFactory$POJOWorkflowImplementation$RootWorkflowInboundCallsInterceptor.execute(POJOWorkflowImplementationFactory.java:289)
	at io.temporal.internal.sync.POJOWorkflowImplementationFactory$POJOWorkflowImplementation.execute(POJOWorkflowImplementationFactory.java:253)
	at io.temporal.internal.sync.WorkflowExecuteRunnable.run(WorkflowExecuteRunnable.java:52)
	at io.temporal.internal.sync.SyncWorkflow.lambda$start$0(SyncWorkflow.java:121)
	at io.temporal.internal.sync.SyncWorkflow$$Lambda$93/0000000000000000.run(Unknown Source)
	at io.temporal.internal.sync.CancellationScopeImpl.run(CancellationScopeImpl.java:104)
	at io.temporal.internal.sync.WorkflowThreadImpl$RunnableWrapper.run(WorkflowThreadImpl.java:111)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	... 3 common frames omitted
```

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [November 21, 2020, 7:21pm UTC](https://community.temporal.io/t/throwing-exception-vs-failure-in-workflow/1059/2 "2020-11-21T19:21:07Z")

</div>

By default, any unexpected exception which doesn’t extend TemporalFailure doesn’t fail a workflow. It blocks its execution and periodically retries waiting for the fix. This is done to avoid failing workflows due to unexpected bugs like NPE. In most cases, users do not want a few million workflows to fail and require manual intervention on a silly NPE bug introduced by a new deployment.

If you indeed want to fail the workflow on a specific exception include it into [WorkflowImplementationOptions.FailWorkflowImplementationTypes](https://github.com/temporalio/sdk-java/blob/30420212a26283cf7cbe6339512058a3186074e8/temporal-sdk/src/main/java/io/temporal/worker/WorkflowImplementationOptions.java#L51). For example, if you want your workflow to fail on any exception (which we very rarely recommend in production) set it to Throwable. The WorkflowImplementationOptions are passed to [Worker.registerWorkflowImplementationTypes](https://github.com/temporalio/sdk-java/blob/8fba0454392a79d7371f103bc8fb084c1be8ae68/temporal-sdk/src/main/java/io/temporal/worker/Worker.java#L245) call.

---

<div class="post-metadata">

**Author:** ![nathan\_shields](https://avatars.discourse-cdn.com/v4/letter/n/bc79bd/32.png) [@nathan\_shields](https://community.temporal.io/u/nathan_shields)\
**Post date:** [June 25, 2021, 7:33pm UTC](https://community.temporal.io/t/throwing-exception-vs-failure-in-workflow/1059/3 "2021-06-25T19:33:04Z")

</div>

> [@maxim](#):
>
> By default, any unexpected exception which doesn’t extend TemporalFailure doesn’t fail a workflow. It blocks its execution and periodically retries waiting for the fix. This is done to avoid failing workflows due to unexpected bugs like NPE. In most cases, users do not want a few million workflows to fail and require manual intervention on a silly NPE bug introduced by a new deployment.

If a Workflow retries due to one of the NPE cases, will it also retry (not **replay** ) any activities that have completed before? I’m wondering what happens if I make a non-idempotent API call in my workflow that is being retried while it waits for a fix.

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [June 25, 2021, 8:21pm UTC](https://community.temporal.io/t/throwing-exception-vs-failure-in-workflow/1059/4 "2021-06-25T20:21:10Z")

</div>

No, activities will not be retried. And workflow doesn’t retry due to NPE. I wasn’t precise. The last _workflow task_ is retried. Not the whole workflow.

---

<div class="post-metadata">

**Author:** ![Kousha\_Talebian](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/kousha_talebian/32/2677_2.png) [@Kousha\_Talebian](https://community.temporal.io/u/Kousha_Talebian)\
**Post date:** [September 28, 2022, 2:32am UTC](https://community.temporal.io/t/throwing-exception-vs-failure-in-workflow/1059/5 "2022-09-28T02:32:21Z")

</div>

I want the workflow to fail for certain exceptions thrown and so I will be using [WorkflowImplementationOptions.FailWorkflowImplementationTypes](https://github.com/temporalio/sdk-java/blob/30420212a26283cf7cbe6339512058a3186074e8/temporal-sdk/src/main/java/io/temporal/worker/WorkflowImplementationOptions.java#L51)

My workflows, however, are async and long-running and I execute them using `submitWorkflow`. How can I create an exceptionMapper/handler to handle these failed workflow executions?

More generally as well, if a workflow fails (due to timeout, etc), can we register a handler that is invoked which then performs cleanup operations?

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [September 28, 2022, 3:22pm UTC](https://community.temporal.io/t/throwing-exception-vs-failure-in-workflow/1059/6 "2022-09-28T15:22:18Z")

</div>

The recommended approach is to perform cleanup by the workflow itself before failing it.

---

<div class="post-metadata">

**Author:** ![Kousha\_Talebian](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/kousha_talebian/32/2677_2.png) [@Kousha\_Talebian](https://community.temporal.io/u/Kousha_Talebian)\
**Post date:** [September 28, 2022, 4:02pm UTC](https://community.temporal.io/t/throwing-exception-vs-failure-in-workflow/1059/7 "2022-09-28T16:02:43Z")

</div>

But is there an `Override` method like `void onError(final Throwable e)` or `void onEnd(final Throwable e)` that we can use? Alternatively, I can basically have a `try/catch` over the entire code if there is no better way.

---

<div class="post-metadata">

**Author:** ![tihomir](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/tihomir/32/6580_2.png) [@tihomir](https://community.temporal.io/u/tihomir)\
**Post date:** [September 28, 2022, 5:43pm UTC](https://community.temporal.io/t/throwing-exception-vs-failure-in-workflow/1059/8 "2022-09-28T17:43:06Z")

</div>

> Alternatively, I can basically have a `try/catch` over the entire code if there is no better way.

Or could use workflow inbound interceptor to catch exceptions and handle there. Just note that execution timeouts are managed by server, so you could not for example catch timed out workflows, would probably need to do that in a client interceptor.

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [September 28, 2022, 6:50pm UTC](https://community.temporal.io/t/throwing-exception-vs-failure-in-workflow/1059/9 "2022-09-28T18:50:08Z")

</div>

I think try/catch over the entire code is the best way, as the catch handler has all the necessary data to perform the cleanup.
