# Using NewDetachedCancellationScope for non-cancellation scenarios

**URL:** <https://community.temporal.io/t/using-newdetachedcancellationscope-for-non-cancellation-scenarios/3693>\
**Category:** Community Support\
**Tags:** general-impl\
**Created:** [January 5, 2022, 6:03pm UTC](https://community.temporal.io/t/using-newdetachedcancellationscope-for-non-cancellation-scenarios/3693 "2022-01-05T18:03:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tempuser](https://avatars.discourse-cdn.com/v4/letter/t/9dc877/32.png) [@tempuser](https://community.temporal.io/u/tempuser)\
**Post date:** [January 5, 2022, 6:03pm UTC](https://community.temporal.io/t/using-newdetachedcancellationscope-for-non-cancellation-scenarios/3693/1 "2022-01-05T18:03:41Z")

</div>

Hi Temporal team,

I’m trying to simplify some Workflow code with regards to cleanup logic:

```auto
try {

// run some activities

} catch (ActivityFailure e) {
// error handling
}
catch (CanceledFailure e) {

// run activity to cleanup a record in DB, using NewDetachedCancellationScope

} finally {

// run activity to cleanup a record in DB

}

```

Basically, I want to run an activity to cleanup a record in DB, whether there was an Activity failure, or if Workflow was canceled, or Workflow completed successfully. This is done in finally.  
Is there any drawback to using NewDetachedCancellationScope in the finally clause?

thanks,  
Richard

---

<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:** [January 5, 2022, 6:47pm UTC](https://community.temporal.io/t/using-newdetachedcancellationscope-for-non-cancellation-scenarios/3693/2 "2022-01-05T18:47:36Z")

</div>

Activity invocations through activity stub throw only ActivityFailure exception. So In case of wf cancellation that happens during activity execution, you will catch ActivityFailure which will have CanceledFailure as its cause. So imo you dont need to catch both ActivityFailure and CanceledFailure failures separately, but for example just catch `ActivityFailure e` and you can check if its CanceledFailure if you need:

```auto
if(e.getCause() instanceof CanceledFailure) {
            // do some cancellation cleanup here
        }

```

Think it’s ok to have newDetachedCancellationScope in finally. In case where you want to do the cleanup only on activity failure or cancellation request, you should be able to handle that by catching ActivityFailure alone ( see sample: [samples-java/HelloDetachedCancellationScope.java at main · temporalio/samples-java · GitHub](https://github.com/temporalio/samples-java/blob/main/src/main/java/io/temporal/samples/hello/HelloDetachedCancellationScope.java#L134))

---

<div class="post-metadata">

**Author:** ![tempuser](https://avatars.discourse-cdn.com/v4/letter/t/9dc877/32.png) [@tempuser](https://community.temporal.io/u/tempuser)\
**Post date:** [January 5, 2022, 9:31pm UTC](https://community.temporal.io/t/using-newdetachedcancellationscope-for-non-cancellation-scenarios/3693/3 "2022-01-05T21:31:04Z")

</div>

Thanks!

In the case of cancel on WorkflowStub, a CanceledFailure would be thrown. So we would still need a separate catch block right?

---

<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:** [January 6, 2022, 3:40am UTC](https://community.temporal.io/t/using-newdetachedcancellationscope-for-non-cancellation-scenarios/3693/4 "2022-01-06T03:40:42Z")

</div>

For your example code you can just catch `TemporalFailure` which is going to be either ActivityFailure (with the CanceledFailure as cause) or CanceledFailure. If it’s ActivityFailure you can perform some compensation logic if needed for the activities (all in detached scope)

```auto
...
} catch (TemporalFailure e) {
   if (e instanceof ActivityFailure) {
      Workflow.newDetachedCancellationScope(() -> ...).run();
  } else if(e instanceof CanceledFailure) {
    Workflow.newDetachedCancellationScope(() -> ...).run();
  } 
 throw e;
}

```

you can also run your cleanup code in finally block as well if needed for your case.

---

<div class="post-metadata">

**Author:** ![tempuser](https://avatars.discourse-cdn.com/v4/letter/t/9dc877/32.png) [@tempuser](https://community.temporal.io/u/tempuser)\
**Post date:** [January 11, 2022, 10:21pm UTC](https://community.temporal.io/t/using-newdetachedcancellationscope-for-non-cancellation-scenarios/3693/5 "2022-01-11T22:21:20Z")

</div>

Thanks!

```auto
...
int foo;

...

} catch (TemporalFailure e) {
   if (e instanceof ActivityFailure) {
      Workflow.newDetachedCancellationScope(() -> ...).run();
  } else if(e instanceof CanceledFailure) {
    Workflow.newDetachedCancellationScope(() -> foo++).run();
  } 
 throw e;
}

```

Is this allowed? Even though foo is declared in root scope which was canceled?

---

<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:** [January 11, 2022, 11:35pm UTC](https://community.temporal.io/t/using-newdetachedcancellationscope-for-non-cancellation-scenarios/3693/6 "2022-01-11T23:35:17Z")

</div>

Yes, see [detached cencellation scope sample](https://github.com/temporalio/samples-java/blob/main/src/main/java/io/temporal/samples/hello/HelloDetachedCancellationScope.java#L137).
