# Custom exception handler

**URL:** https://discourse.julialang.org/t/custom-exception-handler/18946
**Category:** General Usage
**Created:** [December 23, 2018, 5:08pm UTC](https://discourse.julialang.org/t/custom-exception-handler/18946 "2018-12-23T17:08:50Z")
**Posts on this page:** 1
**Showing post:** 6

<div class="post-metadata">

### Author: ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)
#### Post date: [December 26, 2018, 10:42am UTC](https://discourse.julialang.org/t/custom-exception-handler/18946/6 "2018-12-26T10:42:04Z")

</div>

> [@tobias.knopp](#):
>
> This has not so much tied to Gtk but the task machinery itself

If a random C library (call it `libfoo`; this can be gtk or something else entirely unrelated to julia) calls a callback which then calls `longjmp`, it’s very likely that the state of the data structures managed by `libfoo` will be corrupted. That is, _unless_ `libfoo` has been explicitly and very carefully designed to be safe for this kind of thing which is fairly uncommon. There is nothing the julia task runtime can do about this; if you do throw an exception which skips over the stack frames of an external library anything may happen, including memory leaks, resource leaks, random segfaults hours later, or if you’re very lucky nothing bad at all. Don’t rely on it!

> [@tobias.knopp](#):
>
> I would argue that it would be better if the Julia runtime catches all exceptions.

The julia runtime itself has been explicitly and carefully designed to be exception (currently implemented with `longjmp`) safe, but only for `longjmp`s which are managed by julia itself. If you find a case where a native julia exception crashes the julia runtime (with or without a crash dump) this is a bug and should be reported. Just don’t mix julia exceptions in callbacks with external library code and expect it to work 😉 If you mixed C++ exceptions with C longjmp there’s a good chance of memory corruption etc, and `std::terminate` would never be called there either.

---

_[View the full topic](https://discourse.julialang.org/t/custom-exception-handler/18946)._
