# Using exceptions to return from deeply nested recursions?

**URL:** <https://forum.canton.network/t/using-exceptions-to-return-from-deeply-nested-recursions/2783>\
**Category:** App Development\
**Created:** [June 29, 2021, 6:04pm UTC](https://forum.canton.network/t/using-exceptions-to-return-from-deeply-nested-recursions/2783 "2021-06-29T18:04:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Leonid\_Rozenberg](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/leonid_rozenberg/32/48_2.png) [@Leonid\_Rozenberg](https://forum.canton.network/u/Leonid_Rozenberg)\
**Post date:** [June 29, 2021, 6:04pm UTC](https://forum.canton.network/t/using-exceptions-to-return-from-deeply-nested-recursions/2783/1 "2021-06-29T18:04:23Z")

</div>

Now that exceptions are in the language, is there any performance benefit to using them to escape from a long call stack?

In other languages this is a technique that is used to unwind all of the accumulated stack frames in one operation.

---

<div class="post-metadata">

**Author:** ![cocreature](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/cocreature/32/36_2.png) [@cocreature](https://forum.canton.network/u/cocreature)\
**Post date:** [June 30, 2021, 5:20pm UTC](https://forum.canton.network/t/using-exceptions-to-return-from-deeply-nested-recursions/2783/2 "2021-06-30T17:20:41Z")

</div>

I assume you’re comparing it against an `Either`/`EitherT` style short-circuiting?  
I do expect exception throwing to outperform that. However, I think it’s important to clarify a few points/caveats:

1. The two are only equivalent if you do not have any writes to the ledger (clearly a given if we talk about code outside of `Update`. Otherwise the exception will roll them back whereas `Either` will not roll them back.
2. You can only catch in `Update`. I’d think twice about moving things into `Update` just to get a performance boost.
3. You make it potentially harder to understand your code by making the errors not visible in the type.
4. How much it matters really depends on your code. If you have a tight loop which is basically doing nothing more than `>>=` on `Either`, you can probably gain something. However, most code does something interesting between binds and then the performance advantage can easily become insignificant.
