# Julia exceptions in C

**URL:** https://discourse.julialang.org/t/julia-exceptions-in-c/18387
**Category:** General Usage
**Tags:** embedding, exception
**Created:** [December 6, 2018, 5:44pm UTC](https://discourse.julialang.org/t/julia-exceptions-in-c/18387 "2018-12-06T17:44:00Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![vitreo12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vitreo12/32/4028_2.png) [@vitreo12](https://discourse.julialang.org/u/vitreo12)
#### Post date: [December 6, 2018, 5:44pm UTC](https://discourse.julialang.org/t/julia-exceptions-in-c/18387/1 "2018-12-06T17:44:00Z")

</div>

Hi everyone,  
is there a convenient way of retrieving Julia exceptions in the C interface so that they appear the same way that they do on the Julia REPL? Considering a call to an `undefined_function()`, on the REPL the output is :

`ERROR: UndefVarError: undefined_function not defined`.

Following the example on Julia on [Exceptions](https://docs.julialang.org/en/v1/manual/embedding/index.html#Exceptions-1) for C embedded Julia code, the result of

`printf("%s \n", jl_typeof_str(jl_exception_occurred()));`

after a

`jl_eval_string("undefined_function()");`

simply is:

`UndefVarError`

which wouldn’t be much useful in more complicated scenarios. Especially considering exception calls following an `include`, which also need to give informations about errors at specific line numbers, etc…  
Is there a way to retrieve the same kind of detail of Julia exceptions in C without having to rewrite them all with the `jl_type_error` C interface?

---

<div class="post-metadata">

### Author: ![vitreo12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vitreo12/32/4028_2.png) [@vitreo12](https://discourse.julialang.org/u/vitreo12)
#### Post date: [December 6, 2018, 7:45pm UTC](https://discourse.julialang.org/t/julia-exceptions-in-c/18387/2 "2018-12-06T19:45:14Z")

</div>

For anyone wondering, I found a solution using `sprint` and `showerror`:

```julia
    jl_eval_string("undefined_function()");

    jl_value_t* exception = jl_exception_occurred();
    jl_value_t* sprint_fun = jl_get_function(jl_main_module, "sprint");
    jl_value_t* showerror_fun = jl_get_function(jl_main_module, "showerror");
    
    JL_GC_PUSH3(&exception, &sprint_fun, &showerror_fun);

    if(exception)
    {
        const char* returned_exception = jl_string_ptr(jl_call2(sprint_fun, showerror_fun, exception));
        printf("ERROR: %s\n", returned_exception);
    }
    
    JL_GC_POP();

```

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [December 7, 2018, 11:56am UTC](https://discourse.julialang.org/t/julia-exceptions-in-c/18387/3 "2018-12-07T11:56:46Z")

</div>

In [https://github.com/JuliaLang/julia/pull/28886](https://github.com/JuliaLang/julia/pull/28886) this construction is used, which looks fairly similar to your solution.

```julia
    if (jl_exception_occurred()) {
        const char *p = jl_unbox_voidpointer(jl_eval_string("pointer(sprint(showerror, ccall(:jl_exception_occurred, Any, ())))"));
        fprintf(stderr, "%s%s\n", error_message, p);
        return 0;
    }

```

Note that this is for a dynamic loading setting, where the `GC_PUSH` macros aren’t available.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [December 7, 2018, 1:09pm UTC](https://discourse.julialang.org/t/julia-exceptions-in-c/18387/4 "2018-12-07T13:09:55Z")

</div>

Note that there are also `JL_TRY` and `JL_CATCH` macros in C. [Here is an example](https://github.com/JuliaLang/julia/blob/24f1316e91de029f71f636db23aced49156b44ad/ui/repl.c#L32-L62) that shows exceptions with the help of these macros in the REPL source code.

---

<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: [December 7, 2018, 1:26pm UTC](https://discourse.julialang.org/t/julia-exceptions-in-c/18387/5 "2018-12-07T13:26:32Z")

</div>

This is wrong. Don’t call `pointer`.

---

<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: [December 7, 2018, 1:28pm UTC](https://discourse.julialang.org/t/julia-exceptions-in-c/18387/6 "2018-12-07T13:28:56Z")

</div>

The `JL_GC_PUSH3` isn’t doing anything here. You need to do it before calling the next runtime function, i.e. you must use `JL_GC_PUSH3` before calling any of the `jl_get_function`. You should also replace the main module with the base module and you don’t really need to root the two functions.

---

<div class="post-metadata">

### Author: ![vitreo12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vitreo12/32/4028_2.png) [@vitreo12](https://discourse.julialang.org/u/vitreo12)
#### Post date: [December 7, 2018, 3:45pm UTC](https://discourse.julialang.org/t/julia-exceptions-in-c/18387/7 "2018-12-07T15:45:16Z")

</div>

> [@stevengj](#):
>
> Note that there are also `JL_TRY` and `JL_CATCH` macros in C. [Here is an example](https://github.com/JuliaLang/julia/blob/24f1316e91de029f71f636db23aced49156b44ad/ui/repl.c#L32-L62) that shows exceptions with the help of these macros in the REPL source code.

Thank you! That is exactly what I was looking for.

> [@yuyichao](#):
>
> The `JL_GC_PUSH3` isn’t doing anything here. You need to do it before calling the next runtime function, i.e. you must use `JL_GC_PUSH3` before calling any of the `jl_get_function` . You should also replace the main module with the base module and you don’t really need to root the two functions.

Thank you for the feedback. Why is base module better than the main module in this case? And what do you mean by “you don’t really need to root the two functions”?

---

<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: [December 7, 2018, 4:39pm UTC](https://discourse.julialang.org/t/julia-exceptions-in-c/18387/8 "2018-12-07T16:39:36Z")

</div>

> [@vitreo12](#):
>
> Why is base module better than the main module in this case?

Because those names are from base, not main. It can be reassigned in main.

> [@vitreo12](#):
>
> you don’t really need to root the two functions

I mean you don’t need to put those in `JL_GC_PUSH`. They are global constants and are always rooted.

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [December 7, 2018, 6:36pm UTC](https://discourse.julialang.org/t/julia-exceptions-in-c/18387/9 "2018-12-07T18:36:19Z")

</div>

> [@yuyichao](#):
>
> This is wrong. Don’t call `pointer` .

Thanks. Is

```julia
const char *p = jl_string_ptr(jl_eval_string("sprint(showerror, ccall(:jl_exception_occurred, Any, ()))"));

```

fine?

---

<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: [December 7, 2018, 6:48pm UTC](https://discourse.julialang.org/t/julia-exceptions-in-c/18387/10 "2018-12-07T18:48:18Z")

</div>

It’s fine if you make sure you don’t call anything else before finish using that pointer.

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [December 8, 2018, 10:47am UTC](https://discourse.julialang.org/t/julia-exceptions-in-c/18387/11 "2018-12-08T10:47:20Z")

</div>

Yes, it goes out of scope before any other call into Julia. I’ve fixed this in [https://github.com/JuliaLang/julia/pull/28886](https://github.com/JuliaLang/julia/pull/28886). I’d be happy to know what else is wrong in the PR.
