# Is there a way to get the stacktrace from an error object?

**URL:** <https://discourse.julialang.org/t/is-there-a-way-to-get-the-stacktrace-from-an-error-object/73593>\
**Category:** New to Julia\
**Created:** [December 24, 2021, 7:01pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-get-the-stacktrace-from-an-error-object/73593 "2021-12-24T19:01:52Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![RobertN](https://avatars.discourse-cdn.com/v4/letter/r/f0a364/32.png) [@RobertN](https://discourse.julialang.org/u/RobertN)\
**Post date:** [December 24, 2021, 7:01pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-get-the-stacktrace-from-an-error-object/73593/1 "2021-12-24T19:01:52Z")

</div>

I have an object with type `MethodError`. I want to print the stacktrace (ie, backtrace) to see where it occurred. How do I do that? I see a method `backtrace` but it doesn’t take an argument.

`propertynames(err)` reveals only one property: `:msg`.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [December 24, 2021, 7:59pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-get-the-stacktrace-from-an-error-object/73593/2 "2021-12-24T19:59:09Z")

</div>

not really, IIRC the Error object only contains error (what function, what world age, what argument etc.), not the backtrace

---

<div class="post-metadata">

**Author:** ![RobertN](https://avatars.discourse-cdn.com/v4/letter/r/f0a364/32.png) [@RobertN](https://discourse.julialang.org/u/RobertN)\
**Post date:** [December 24, 2021, 10:11pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-get-the-stacktrace-from-an-error-object/73593/3 "2021-12-24T22:11:33Z")

</div>

Okay. I’m trying to embed Julia. If I run a file of Julia code, from C code, and I get an error back (from `jl_exception_occured`), it seems I have no way to print the stack trace or useful source location info.

---

<div class="post-metadata">

**Author:** ![rikh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rikh/32/204104_2.png) [@rikh](https://discourse.julialang.org/u/rikh)\
**Post date:** [December 24, 2021, 10:57pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-get-the-stacktrace-from-an-error-object/73593/4 "2021-12-24T22:57:16Z")

</div>

Depending on the precise situation, maybe `rethrow` can help you out. But yeah the error object doesn’t contain the full stacktrace

---

<div class="post-metadata">

**Author:** ![RobertN](https://avatars.discourse-cdn.com/v4/letter/r/f0a364/32.png) [@RobertN](https://discourse.julialang.org/u/RobertN)\
**Post date:** [December 25, 2021, 3:43am UTC](https://discourse.julialang.org/t/is-there-a-way-to-get-the-stacktrace-from-an-error-object/73593/5 "2021-12-25T03:43:09Z")

</div>

Aside from a full stacktrace, does it contain any source location info at all? It seems, no. And if that’s the case, I don’t see how you can embed a non-trivial amount of Julia code.

---

<div class="post-metadata">

**Author:** ![anon56330260](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@anon56330260](https://discourse.julialang.org/u/anon56330260)\
**Post date:** [December 25, 2021, 4:48am UTC](https://discourse.julialang.org/t/is-there-a-way-to-get-the-stacktrace-from-an-error-object/73593/6 "2021-12-25T04:48:57Z")

</div>

Julia will scan the current execution stack when encountering errors. The source location and other debug information are allocated and set up with the stack frame. It seems that there’s a experimental function `Base.catch_stack` which is suitable for your task. But you still need to wrap your code in a try-catch-block to provide an exception handle.

---

<div class="post-metadata">

**Author:** ![RobertN](https://avatars.discourse-cdn.com/v4/letter/r/f0a364/32.png) [@RobertN](https://discourse.julialang.org/u/RobertN)\
**Post date:** [December 25, 2021, 5:12am UTC](https://discourse.julialang.org/t/is-there-a-way-to-get-the-stacktrace-from-an-error-object/73593/7 "2021-12-25T05:12:07Z")

</div>

> [@anon56330260](#):
>
> It seems that there’s a experimental function `Base.catch_stack` which is suitable for your task. But you still need to wrap your code in a try-catch-block to provide an exception handle.

Do you mean wrap in try/catch in C code? Something like this? (not working)

```plaintext
JL_TRY {
    jl_value_t *result = jl_eval_string(code);
    // result in null, catch block does not run
}
JL_CATCH {
    jl_printf((JL_STREAM*)STDERR_FILENO, "ERROR:\n");
    jl_static_show((JL_STREAM*)STDERR_FILENO, jl_current_exception());
    jl_printf((JL_STREAM*)STDERR_FILENO, "\n");
    jl_print_backtrace(); // backtrace(); // written to STDERR_FILENO
}

```

I copied that use of the JL\_TRY and JL\_CATCH macros from Julia’s C code.

---

<div class="post-metadata">

**Author:** ![anon56330260](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@anon56330260](https://discourse.julialang.org/u/anon56330260)\
**Post date:** [December 25, 2021, 5:35am UTC](https://discourse.julialang.org/t/is-there-a-way-to-get-the-stacktrace-from-an-error-object/73593/8 "2021-12-25T05:35:30Z")

</div>

I guess `jl_eval_string` already has a try-catch-block and simply ignores the error for you, so you need to dig into `jl_eval_string` further to find out the actual function.

---

<div class="post-metadata">

**Author:** ![anon56330260](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@anon56330260](https://discourse.julialang.org/u/anon56330260)\
**Post date:** [December 25, 2021, 8:22am UTC](https://discourse.julialang.org/t/is-there-a-way-to-get-the-stacktrace-from-an-error-object/73593/9 "2021-12-25T08:22:24Z")

</div>

```julia
JL_DLLEXPORT jl_value_t *jl_eval_string(const char *str)
{
    jl_value_t *r;
    JL_TRY {
        const char filename[] = "none";
        jl_value_t *ast = jl_parse_all(str, strlen(str),
                filename, strlen(filename));
        JL_GC_PUSH1(&ast);
        r = jl_toplevel_eval_in(jl_main_module, ast);
        JL_GC_POP();
        jl_exception_clear();
    }
    JL_CATCH {
        jl_current_task->ptls->previous_exception = jl_current_exception();
        r = NULL;
    }
    return r;
}

```

This is the definition of `jl_eval_string`. Simply duplicating this function and adding your exception handling code to `JL_CATCH` should be fine.
