# How do I throw an Exception with a given backtrace?

**URL:** <https://discourse.julialang.org/t/how-do-i-throw-an-exception-with-a-given-backtrace/47940>\
**Category:** General Usage\
**Created:** [October 7, 2020, 2:36pm UTC](https://discourse.julialang.org/t/how-do-i-throw-an-exception-with-a-given-backtrace/47940 "2020-10-07T14:36:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [October 7, 2020, 2:36pm UTC](https://discourse.julialang.org/t/how-do-i-throw-an-exception-with-a-given-backtrace/47940/1 "2020-10-07T14:36:39Z")

</div>

If I have aquired a backtrace, e.g. from `backtrace()` and i want to throw an exception that impersonates an exception being thrown with that backtrace, how do i do it?

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [October 7, 2020, 2:39pm UTC](https://discourse.julialang.org/t/how-do-i-throw-an-exception-with-a-given-backtrace/47940/2 "2020-10-07T14:39:10Z")

</div>

found in the julia source code marked a pseudocode:

```julia
function throw(val)
     global catch_backtrace = backtrace()
     rethrow(val)
end

```

Now sure that will work though

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [October 7, 2020, 4:08pm UTC](https://discourse.julialang.org/t/how-do-i-throw-an-exception-with-a-given-backtrace/47940/3 "2020-10-07T16:08:21Z")

</div>

Couldn’t you just create your own exception type to wrap the actually thrown expression and your fake backtrace (also define `display_backtrace` etc for proper printing and so on)?

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [October 7, 2020, 4:33pm UTC](https://discourse.julialang.org/t/how-do-i-throw-an-exception-with-a-given-backtrace/47940/4 "2020-10-07T16:33:21Z")

</div>

yeah, that would do it.  
I thought there might be an inbuilt way.  
since `rethrow(e)` basically does this.  
Throwning the new exception `e` with the old backtrace, right?
