# Debug.todo “uncatchable” exception?

**URL:** <https://discourse.elm-lang.org/t/debug-todo-uncatchable-exception/4270>\
**Category:** Learn\
**Created:** [September 6, 2019, 2:32am UTC](https://discourse.elm-lang.org/t/debug-todo-uncatchable-exception/4270 "2019-09-06T02:32:38Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![kevinyank](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/kevinyank/32/241_2.png) [@kevinyank](https://discourse.elm-lang.org/u/kevinyank)\
**Post date:** [September 6, 2019, 2:32am UTC](https://discourse.elm-lang.org/t/debug-todo-uncatchable-exception/4270/1 "2019-09-06T02:32:38Z")

</div>

In the docs for `Debug.todo` (and `Debug.crash` before it), it says:

> The Elm compiler recognizes each `Debug.todo` so if you run into it, you get an **uncatchable runtime exception** that includes the module name and line number.

I’d like to understand what the technical meaning of “uncatchable” is in this sentence.

For context, we’ve been progressively upgrading our Elm 0.18 codebase to Elm 0.19. We have yet to enable the `--optimize` flag for our Elm 0.19 builds, because in a number of places we are using code like this:

```elm
case something of
    Value1 ->
        -- …

    Value2 ->
        -- …

    ImpossibleValue ->
        Debug.todo "This is meant to be impossible."

```

Of course we do our best to “make impossible states impossible” using the type system, but every once in awhile, despite our best efforts to model our data and state accurately, we do end up having to provide an implementation for a message that we expect we’ll never get. Of course if we turn out to be wrong about this, we want to find out, so we had been using `Debug.crash` in Elm 0.18 (which is now `Debug.todo` in Elm 0.19) to throw an error that we hoped would get picked up by our JavaScript error monitoring.

That said, so far we have yet to see any such errors turn up in our monitoring, and I’m wondering if this word “uncatchable” might have something to do with it. Is there something special about the way `Debug.todo` works that might prevent a JavaScript error reporting library from responding to and notifying us of the error?

For anyone who might be wondering the “right” way to do this, we do have some of our Elm apps well integrated with our error tracking via a `port`. We aim to roll this approach out across all our codebases in time.

---

<div class="post-metadata">

**Author:** ![joelq](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/joelq/32/445_2.png) [@joelq](https://discourse.elm-lang.org/u/joelq)\
**Post date:** [September 6, 2019, 2:57am UTC](https://discourse.elm-lang.org/t/debug-todo-uncatchable-exception/4270/2 "2019-09-06T02:57:54Z")

</div>

Looking at the source both both [`Debug.crash` (0.18)](https://github.com/elm-lang/core/blob/5.1.1/src/Native/Debug.js#L20-L23) and [`Debug.todo` (0.19)](https://github.com/elm/core/blob/1.0.2/src/Elm/Kernel/Debug.js#L233-L236), they basically do:

```auto
throw new Error(message);

```

under the hood. Not sure if that’s catcheable on the JS side or not.

---

<div class="post-metadata">

**Author:** ![kevinyank](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/kevinyank/32/241_2.png) [@kevinyank](https://discourse.elm-lang.org/u/kevinyank)\
**Post date:** [September 6, 2019, 3:15am UTC](https://discourse.elm-lang.org/t/debug-todo-uncatchable-exception/4270/3 "2019-09-06T03:15:55Z")

</div>

Seems catchable to me:

```auto
try {
	throw new Error("this is a test");
} catch (e) {
	console.log("I caught it: " + e.toString());
}
// [Log] I caught it: Error: this is a test

```

---

<div class="post-metadata">

**Author:** ![akoppela](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/akoppela/32/250_2.png) [@akoppela](https://discourse.elm-lang.org/u/akoppela)\
**Post date:** [September 6, 2019, 6:36am UTC](https://discourse.elm-lang.org/t/debug-todo-uncatchable-exception/4270/4 "2019-09-06T06:36:01Z")

</div>

I think it means that it’s uncatchable from within the Elm. Definitely you can catch it on JS side and do wary ever you want with it, e.g. log the error, or send it back to Elm via ports.

---

<div class="post-metadata">

**Author:** ![lydell](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/lydell/32/178_2.png) [@lydell](https://discourse.elm-lang.org/u/lydell)\
**Post date:** [September 6, 2019, 6:42pm UTC](https://discourse.elm-lang.org/t/debug-todo-uncatchable-exception/4270/5 "2019-09-06T18:42:16Z")

</div>

I don’t think you can use `try {...} catch (error) {}` to catch errors thrown by Elm, since they happen asynchronously. They should however appear in [window.onerror](https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onerror#window.onerror).

---

<div class="post-metadata">

**Author:** ![kevinyank](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/kevinyank/32/241_2.png) [@kevinyank](https://discourse.elm-lang.org/u/kevinyank)\
**Post date:** [September 11, 2019, 2:43am UTC](https://discourse.elm-lang.org/t/debug-todo-uncatchable-exception/4270/6 "2019-09-11T02:43:44Z")

</div>

Having done some testing, we’ve found that our JavaScript error notification library (Bugsnag) does indeed catch and report `Debug.todo` errors from Elm. While replacing these errors with commands sent to a port would enable us to use the `--optimize` flag, we would lose the module name and line number in the error message that we get now. We may still choose to do this for the benefits of `--optimize`.

---

<div class="post-metadata">

**Author:** ![pdamoc](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/pdamoc/32/36_2.png) [@pdamoc](https://discourse.elm-lang.org/u/pdamoc)\
**Post date:** [September 11, 2019, 6:00am UTC](https://discourse.elm-lang.org/t/debug-todo-uncatchable-exception/4270/7 "2019-09-11T06:00:51Z")

</div>

> [@kevinyank](#):
>
> While replacing these errors with commands sent to a port would enable us to use the `--optimize` flag, we would lose the module name and line number in the error message that we get now.

I usually have a String field with the location of the port error logging where I put the name of the module and the Tag where this error occurred. That is plenty information in order to trace it back since this type of code should be very infrequent.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [September 21, 2019, 6:00am UTC](https://discourse.elm-lang.org/t/debug-todo-uncatchable-exception/4270/8 "2019-09-21T06:00:53Z")

</div>

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
