# Capture exception in a rescue block

**URL:** <https://www.ruby-forum.com/t/capture-exception-in-a-rescue-block/215924>\
**Category:** Ruby-Gnome 2\
**Created:** [January 19, 2012, 2:27pm UTC](https://www.ruby-forum.com/t/capture-exception-in-a-rescue-block/215924 "2012-01-19T14:27:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [January 19, 2012, 2:27pm UTC](https://www.ruby-forum.com/t/capture-exception-in-a-rescue-block/215924/1 "2012-01-19T14:27:46Z")

</div>

Hi

I’m trying to write the backtrace of an error to a file, but all I’m  
getting is a SystemExit from glib2. While on the terminal, I have the  
usefull backtrace.

I managed to find

> <https://stackoverflow.com/questions/1466577/ruby-rescue-to-display-full-backtrace>

, but there is no solution.

From what I understand, GLib captures all exceptions, and reroutes them  
to GLib::exit\_application. This method outputs things to $stderr before  
exiting the ruby program.

I have 2 questions :

- why does it work that way ?
- Why does the method calls ‘exit’ instead of reraising the exception ?

The method seems to only duplicate the default ruby backtrace, without  
anything added.

regards

Simon

---

<div class="post-metadata">

**Author:** ![mazwak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mazwak](https://www.ruby-forum.com/u/mazwak)\
**Post date:** [January 19, 2012, 2:46pm UTC](https://www.ruby-forum.com/t/capture-exception-in-a-rescue-block/215924/2 "2012-01-19T14:46:53Z")

</div>

Hi,

In [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
“[ruby-gnome2-devel-en] Capture exception in a rescue block” on Thu,  
19 Jan 2012 14:27:46 +0100,  
Simon A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > From what I understand, GLib captures all exceptions, and reroutes them  
> > to GLib::exit\_application. This method outputs things to $stderr before  
> > exiting the ruby program.
> 
> I have 2 questions :
> 
> - why does it work that way ?

We can’t recover from the situation. Showing backtrace is  
all what we can do.

GLib.exit\_application is only called when an exception is  
raised in a callback. It means that an exception is raised  
in C world not Ruby world. If it’s raised in Ruby world, we  
can recover the situation because Ruby knows how Ruby’s  
exception is handled. But if it’s raised in C world, we  
don’t have any potal recover way. It’s the reason why we  
can’t recover from the situation.

> - Why does the method calls ‘exit’ instead of reraising the exception ?

It’s the same reason menthiond in the above.

You should handle an exception in your callback.

## Thanks,

kou

---

<div class="post-metadata">

**Author:** ![mazwak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mazwak](https://www.ruby-forum.com/u/mazwak)\
**Post date:** [January 19, 2012, 3:17pm UTC](https://www.ruby-forum.com/t/capture-exception-in-a-rescue-block/215924/3 "2012-01-19T15:17:03Z")

</div>

Kouhei S. wrote in post #1041645:

> We can’t recover from the situation. Showing backtrace is  
> all what we can do.  
> GLib.exit\_application is only called when an exception is  
> raised in a callback. It means that an exception is raised  
> in C world not Ruby world. If it’s raised in Ruby world, we  
> can recover the situation because Ruby knows how Ruby’s  
> exception is handled. But if it’s raised in C world, we  
> don’t have any potal recover way. It’s the reason why we  
> can’t recover from the situation.

You mean it would be bad to allow people to rescue from such an  
exception ? So you ensure you are exiting the ruby script ?  
Because, if you replace exit\_application code with just raise, it works  
to get the backtrace.

> > - Why does the method calls ‘exit’ instead of reraising the exception ?
> 
> It’s the same reason menthiond in the above.
> 
> You should handle an exception in your callback.

Well, my primary reason to do this is to log exceptions caused by the  
users so I can debug them later. But I think I will just redirect stderr  
to a file.

Thanks Kou

Simon

---

<div class="post-metadata">

**Author:** ![mazwak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mazwak](https://www.ruby-forum.com/u/mazwak)\
**Post date:** [January 20, 2012, 2:38pm UTC](https://www.ruby-forum.com/t/capture-exception-in-a-rescue-block/215924/4 "2012-01-20T14:38:27Z")

</div>

Kouhei S. wrote in post #1041758:

> Hi,
> 
> In [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> “Re: [ruby-gnome2-devel-en] Capture exception in a rescue block” on  
> Thu, 19 Jan 2012 15:17:03 +0100,  
> Simon A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > You mean it would be bad to allow people to rescue from such an  
> > exception ? So you ensure you are exiting the ruby script ?  
> > Because, if you replace exit\_application code with just raise, it works  
> > to get the backtrace.
> 
> Could you show an example script?

Here are 3 samples.

they override GLib::exit\_application to use :  
raise  
raise exception  
(nothing)

The first two shows the backtrace. You can get rid if the rescue close,  
and it will show the usual ruby backtrace, or add raise at the end of  
the rescue.  
The third one does nothing, as expected.

Tried with 1.9.3 and 1.8.7.

Simon

---

<div class="post-metadata">

**Author:** ![mazwak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mazwak](https://www.ruby-forum.com/u/mazwak)\
**Post date:** [January 20, 2012, 11:16am UTC](https://www.ruby-forum.com/t/capture-exception-in-a-rescue-block/215924/5 "2012-01-20T11:16:54Z")

</div>

Hi,

In [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
“Re: [ruby-gnome2-devel-en] Capture exception in a rescue block” on  
Thu, 19 Jan 2012 15:17:03 +0100,  
Simon A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> You mean it would be bad to allow people to rescue from such an  
> exception ? So you ensure you are exiting the ruby script ?  
> Because, if you replace exit\_application code with just raise, it works  
> to get the backtrace.

Could you show an example script?

## Thanks,

kou

---

<div class="post-metadata">

**Author:** ![mazwak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mazwak](https://www.ruby-forum.com/u/mazwak)\
**Post date:** [January 22, 2012, 4:47am UTC](https://www.ruby-forum.com/t/capture-exception-in-a-rescue-block/215924/6 "2012-01-22T04:47:19Z")

</div>

Hi,

In [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
“Re: [ruby-gnome2-devel-en] Capture exception in a rescue block” on  
Fri, 20 Jan 2012 14:38:27 +0100,  
Simon A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > You mean it would be bad to allow people to rescue from such an  
> > > exception ? So you ensure you are exiting the ruby script ?  
> > > Because, if you replace exit\_application code with just raise, it works  
> > > to get the backtrace.
> > 
> > Could you show an example script?
> 
> Here are 3 samples.

Thanks.

> they override GLib::exit\_application to use :  
> raise  
> raise exception  
> (nothing)
> 
> The first two shows the backtrace. You can get rid if the rescue close,  
> and it will show the usual ruby backtrace, or add raise at the end of  
> the rescue.  
> The third one does nothing, as expected.

Gtk::Button’s clicked signal is a no problem case. Because  
the signal doesn’t require return value. (We may be able to  
avoid calling #exit\_application for those signal handlers.)

There are many signals that requires return value. For  
example, “expose-event” requires boolean return value. If we  
got an exception, we can’t chose what value should be  
returned.

Another example is GLib::Timeout.add, GLib::Idle.add and so  
on but they don’t call #exit\_application. :\< We should fix  
them by calling rbgutil\_invoke\_callback()…

## Thanks,

kou
