# Exception safety

**URL:** <https://forum.crystal-lang.org/t/exception-safety/6604>\
**Category:** Help & Support\
**Created:** [February 16, 2024, 3:49pm UTC](https://forum.crystal-lang.org/t/exception-safety/6604 "2024-02-16T15:49:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidmatter](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/davidmatter/32/2273_2.png) [@davidmatter](https://forum.crystal-lang.org/u/davidmatter)\
**Post date:** [February 16, 2024, 3:49pm UTC](https://forum.crystal-lang.org/t/exception-safety/6604/1 "2024-02-16T15:49:47Z")

</div>

Hey everyone,

I fell a bit in love with crystal.

The only thing that I’m left worrying about is exception safety of third-party code, including stdlib. i.e. how do I know if a method I’m calling raises? And how do I know which type of exceptions it could raise?

Even if I’d read the actual source of the method I’m calling and it doesn’t raise: what if the method calls other methods that can raise? Or worse: What if the method changes in the future and starts to raise?

How do you guys handle this? Is there any static analyser that provides guardrails like this or is there even an initiative regarding adding it to the compiler?

So many questions 😀  
Cheers

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [February 16, 2024, 4:00pm UTC](https://forum.crystal-lang.org/t/exception-safety/6604/2 "2024-02-16T16:00:13Z")

</div>

> [@davidmatter](#):
>
> how do I know if a method I’m calling raises? And how do I know which type of exceptions it could raise?

At this point the only way is by either looking at the source, or more ideally the author documents that in the API docs of the method.

> [@davidmatter](#):
>
> Or worse: What if the method changes in the future and starts to raise?

Keep an eye on the changelog I guess 🤷.

> [@davidmatter](#):
>
> How do you guys handle this?

It hasn’t really been a big deal for me so far. Most methods that raise also have a version that returns `nil`. E.g. if a hash key doesn’t exist, etc. So depending on the context I’ll properly handle the case where it doesn’t exist to avoid surprises. Otherwise if there is an unexpected exception I’d usually find it in specs, or worst case see it in logs and just fix it, with my application gracefully handling unexpected exceptions; either from an edge case, or legitimate bug that was missed.

> [@davidmatter](#):
>
> Is there any static analyser that provides guardrails like this or is there even an initiative regarding adding it to the compiler?

There are some issues related to this tho:

- [Exceptions raised by a method · Issue #8353 · crystal-lang/crystal · GitHub](https://github.com/crystal-lang/crystal/issues/8353)
- [[Discussion] Knowing all possible errors · Issue #7154 · crystal-lang/crystal · GitHub](https://github.com/crystal-lang/crystal/issues/7154)

---

<div class="post-metadata">

**Author:** ![davidmatter](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/davidmatter/32/2273_2.png) [@davidmatter](https://forum.crystal-lang.org/u/davidmatter)\
**Post date:** [February 16, 2024, 4:14pm UTC](https://forum.crystal-lang.org/t/exception-safety/6604/3 "2024-02-16T16:14:38Z")

</div>

Thanks!

Thinking about creating a static analyser rule for that.

Best case scenario for me, speaking as a beginner in crystal, would be a `def method raises` language expression. 🙂

---

<div class="post-metadata">

**Author:** ![Xen](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/xen/32/2288_2.png) [@Xen](https://forum.crystal-lang.org/u/Xen)\
**Post date:** [February 16, 2024, 11:21pm UTC](https://forum.crystal-lang.org/t/exception-safety/6604/4 "2024-02-16T23:21:11Z")

</div>

You relax and learn to love the uncertainty…

In anything but the most simple of apps you cannot possibly handle every possibly error. How to handle an out of memory error in the SSL layer? You don’t, you accept everything will go down in flames if that happens. You have some strategically placed rescues that print a nice error message or tries to log it somewhere. The specific exceptions you might want to catch and handle are usually documented. As for the rest: there’s usually not much you can do about it than establish the fact that the code called couldn’t do what you asked it to do, and that means your code can’t either, so there’s often not much else to do than close up shop.

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [February 20, 2024, 10:21am UTC](https://forum.crystal-lang.org/t/exception-safety/6604/5 "2024-02-20T10:21:34Z")

</div>

I agree on that.  
Exceptions are exceptional. You might want to catch some kinds of exceptions at some strategic points in your program, in order to allow it to recover and move on somehow.

In some languages, exceptions are used more for purposes of directing control flow between layers of a program. Not sure if you’re coming from that angle. But this is typically avoided in Crystal because exceptions have quite a high overhead cost.
