# Getting a stack trace with function argument values

**URL:** https://discourse.julialang.org/t/getting-a-stack-trace-with-function-argument-values/529
**Category:** General Usage
**Tags:** question
**Created:** [November 23, 2016, 4:07pm UTC](https://discourse.julialang.org/t/getting-a-stack-trace-with-function-argument-values/529 "2016-11-23T16:07:42Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [November 23, 2016, 4:07pm UTC](https://discourse.julialang.org/t/getting-a-stack-trace-with-function-argument-values/529/1 "2016-11-23T16:07:42Z")

</div>

When an error is not caught, Julia shows a stack trace with the method signatures. This is informative, but sometimes the actual arguments to the functions would be very useful too. Is there a way to have them shown? Using 0.5.0.

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [November 23, 2016, 4:14pm UTC](https://discourse.julialang.org/t/getting-a-stack-trace-with-function-argument-values/529/2 "2016-11-23T16:14:20Z")

</div>

No. This is the job of the debugger. See my replies in [Inspecting the stack](https://discourse.julialang.org/t/inspecting-the-stack/376)

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [January 9, 2018, 1:11pm UTC](https://discourse.julialang.org/t/getting-a-stack-trace-with-function-argument-values/529/3 "2018-01-09T13:11:10Z")

</div>

I was reading [the manual on stack traces](https://docs.julialang.org/en/latest/manual/stacktraces/) and thought of this issue again. My understanding is that I can get the stack trace with `try ... catch`, eg in this MWE:

```julia
using Compat # for two-argument DomainError in 0.6

bad(x) = throw(DomainError(x, "I don't like this value."))

st = try
    bad(1)
catch
    catch_stacktrace()
end

```

Is there a way of extracting `1` (=`x`) and `"I don't like this value."` from it?

Also, if I happened to just run into an error, and was not prepared with a `try ... catch` like above, there is no way I can extract this information _ex post_; is this correct?

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [January 9, 2018, 1:21pm UTC](https://discourse.julialang.org/t/getting-a-stack-trace-with-function-argument-values/529/4 "2018-01-09T13:21:42Z")

</div>

An exception can store whatever state it wants and that can be retrieved by catching the exception and inspecting it.

The stacktrace itself does, however, not store an instance of the exception.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [January 9, 2018, 1:27pm UTC](https://discourse.julialang.org/t/getting-a-stack-trace-with-function-argument-values/529/5 "2018-01-09T13:27:38Z")

</div>

What is the correct way to use the arguments of `DomainError` then? They are not displayed for uncaught errors, and it seems I cannot extract them either.

Are you suggesting that exceptions save global state somewhere? Wouldn’t this only work when the user has control over the source of the exception?

I am aware that I could

```julia
bad(x) = throw(ArgumentError("I don't like this value."))
st = try
    bad(1)
catch e
    println(e)
end

```

but the thing about exceptions is that I don’t usually expect them, then I may have to rerun a long, potentially nondeterministic computation to get the information about the problem.

---

<div class="post-metadata">

### Author: ![antoine-levitt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/antoine-levitt/32/4008_2.png) [@antoine-levitt](https://discourse.julialang.org/u/antoine-levitt)
#### Post date: [January 9, 2018, 1:32pm UTC](https://discourse.julialang.org/t/getting-a-stack-trace-with-function-argument-values/529/6 "2018-01-09T13:32:41Z")

</div>

> but the thing about exceptions is that I don’t usually expect them, then I may have to rerun a long, potentially nondeterministic computation to get the information about the problem.

That’d be a very nice feature for a debugger. ipython does this with %debug.

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [January 9, 2018, 1:35pm UTC](https://discourse.julialang.org/t/getting-a-stack-trace-with-function-argument-values/529/7 "2018-01-09T13:35:16Z")

</div>

You asked if you could extract the information from the `DomainError`. The answer is yes, if you catch it you can extract that information.

If you want to arbitrarily inspect the stack when an error got thrown, then yuyichao’s answer applies.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [January 9, 2018, 1:43pm UTC](https://discourse.julialang.org/t/getting-a-stack-trace-with-function-argument-values/529/8 "2018-01-09T13:43:20Z")

</div>

Just to make sure I understand: is that answer “no, you can’t, it is impossible by design”? This is the impression I got from the other thread.

OTOH it should be fairly simple to do something like

```julia
julia> macro print_exception(expr)
           quote
               try
                   $expr
               catch e
                   println("Oops, got this exception:")
                   println(e)
               end
           end
       end
@print_exception (macro with 1 method)

julia> @print_exception bad(1)
Oops, got this exception:
ArgumentError("I don't like this value.")

```

and I wonder if the REPL could do something like this by default.

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [January 9, 2018, 2:06pm UTC](https://discourse.julialang.org/t/getting-a-stack-trace-with-function-argument-values/529/9 "2018-01-09T14:06:48Z")

</div>

What do you want to do?? The REPL **IS** printing the error for you by default???

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [January 9, 2018, 2:13pm UTC](https://discourse.julialang.org/t/getting-a-stack-trace-with-function-argument-values/529/10 "2018-01-09T14:13:10Z")

</div>

Ideally,

1. I want embed information in the exception that can be used to isolate and debug the error,
2. I want to be able to use this even if I was not prepared for the error (eg in interactive use).

Printing is fine when the conversion to text and back is sufficient, but that is not always the case (eg when non-printed digits matter).

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [January 9, 2018, 3:13pm UTC](https://discourse.julialang.org/t/getting-a-stack-trace-with-function-argument-values/529/11 "2018-01-09T15:13:30Z")

</div>

> [@Tamas\_Papp](#):
>
> I want embed information in the exception that can be used to isolate and debug the error,

Yes, this is just a normal exception, for example a BoundsError embeds the array and what index was accessed.

> [@Tamas\_Papp](#):
>
> I want to be able to use this even if I was not prepared for the error (eg in interactive use).

When an exception is thrown, the REPL prints the exception and the exception can display the error in whatever way it wants.

What you can’t do is to poke around in the local state of an arbitrary location that throws an arbitrary exception. For that you need something like a debugger.

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [January 10, 2018, 10:35am UTC](https://discourse.julialang.org/t/getting-a-stack-trace-with-function-argument-values/529/12 "2018-01-10T10:35:36Z")

</div>

> [@Tamas\_Papp](#):
>
> I want to be able to use this even if I was not prepared for the error

If you mean you want to access it in a similar fashion as `ans` for the result and **NOT** what your `@print_exception` is doing, then I think that’s a legit and potentially handy feature for the REPL.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [January 10, 2018, 11:06am UTC](https://discourse.julialang.org/t/getting-a-stack-trace-with-function-argument-values/529/13 "2018-01-10T11:06:30Z")

</div>

Yes, this would solve my problem. Thanks for clarifying.
