# Error messages from Gtk callbacks are not being shown

**URL:** https://discourse.julialang.org/t/error-messages-from-gtk-callbacks-are-not-being-shown/38412
**Category:** General Usage
**Created:** [April 29, 2020, 8:18am UTC](https://discourse.julialang.org/t/error-messages-from-gtk-callbacks-are-not-being-shown/38412 "2020-04-29T08:18:43Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![xor0110](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xor0110/32/7926_2.png) [@xor0110](https://discourse.julialang.org/u/xor0110)
#### Post date: [April 29, 2020, 8:18am UTC](https://discourse.julialang.org/t/error-messages-from-gtk-callbacks-are-not-being-shown/38412/1 "2020-04-29T08:18:43Z")

</div>

I’m developing a Gkt.jl application, and errors that happen inside a callback are not being displayed. For example, if you run this code and click the button multiple times:

```julia
using Gtk

w = Gtk.Window()
b = Gtk.Button("hello")
push!(w, b)

x = 0

signal_connect(b, "clicked") do widget
    global x += 1
    @info "hi $x"
    @show 32 ÷ (3 - x)
end

Gtk.showall(w)

```

You’ll see this:

```julia
julia> include("/home/user/errtest.jl");

julia> [ Info: hi 1
32 ÷ (3 - x) = 16
[ Info: hi 2
32 ÷ (3 - x) = 32
[ Info: hi 3
[ Info: hi 4
32 ÷ (3 - x) = -32

```

I understand that Julia sometimes defaults to hiding error messages from secondary threads. Is this something related, or is this specific to Gtk? Is it a bug or a feature? I find it strange because in some situations I do get to see error messages. Would that mean the code was running in the main thread?

I get it that sometimes this behavior is the best, and that forcing every error message to be printed may even be challenging to do. I would absolutely love to see all my error messages, though, since it does not help me when something is not working and I cannot get a clue that it was caused just by a silly typo in a function call, for instance.

What can I do to be able to see all the error messages I may get from every thread in my Gtk.jl application?

---

<div class="post-metadata">

### Author: ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)
#### Post date: [April 29, 2020, 7:38pm UTC](https://discourse.julialang.org/t/error-messages-from-gtk-callbacks-are-not-being-shown/38412/2 "2020-04-29T19:38:30Z")

</div>

I ran into this issue when I first started playing with Julia. What I ended up doing was created a function:

```julia
function log_exception(ex, stack)
    local buffer = IOBuffer()
    showerror(buffer, ex)
    write(buffer, "\n\n")
    for bt in stacktrace(stack)
        showerror(buffer, bt)
        write(buffer, '\n')
    end
    seekstart(buffer)
    println(read(buffer, String))
end

```

Then in ALL my callbacks did:

```julia
try
    # Call back code
catch ex
    log_exception(ex, catch_backtrace())
end

```

I did this back when I first started playing with Julia. So the `println(read(buffer, String))` can probably be optimized to `println(take!(buffer))` in the log\_exception function. And a macro could probably be created to do the try/catch.

---

<div class="post-metadata">

### Author: ![xor0110](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xor0110/32/7926_2.png) [@xor0110](https://discourse.julialang.org/u/xor0110)
#### Post date: [May 4, 2020, 9:47am UTC](https://discourse.julialang.org/t/error-messages-from-gtk-callbacks-are-not-being-shown/38412/3 "2020-05-04T09:47:28Z")

</div>

Thanks so much @pixel27, this is very helpful. Indeed, looks like it could be a useful macro.

I imagine many programmers should feel the same need. I’m not sure how we could take this upstream, though.

---

<div class="post-metadata">

### Author: ![xor0110](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xor0110/32/7926_2.png) [@xor0110](https://discourse.julialang.org/u/xor0110)
#### Post date: [June 22, 2020, 3:27pm UTC](https://discourse.julialang.org/t/error-messages-from-gtk-callbacks-are-not-being-shown/38412/4 "2020-06-22T15:27:26Z")

</div>

I finally got to writing that macro. What’s a good place to submit this? Gtk.jl? Julia itself? Maybe it will just live on forever as a hacky snippet on discourse…

```julia
"Catches failures and prints a backtrace, the \"normal\" behavior that is sometimes suppressed in Julia"
macro printbt(f)
    f.args[2] = quote
        try
            $(f.args[2])
        catch ex
            stack = catch_backtrace()
            local buffer = IOBuffer()
            showerror(buffer, ex)
            write(buffer, "\n\n")
            for bt in stacktrace(stack)
                showerror(buffer, bt)
                write(buffer, '\n')
            end
            seekstart(buffer)
            println(read(buffer, String))
        end
    end
    f
end

```
