Forum: Ruby-Gnome 2 Capture exception in a rescue block

Posted by Simon Arnaud (sarnaud)
on 2012-01-19 14:27
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
http://stackoverflow.com/questions/1466577/ruby-re...
, 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
Posted by Kouhei Sutou (Guest)
on 2012-01-19 14:46
(Received via mailing list)
Hi,

In <e6cfa2c17746b9cb237a09eae49e895a@ruby-forum.com>
  "[ruby-gnome2-devel-en] Capture exception in a rescue block" on Thu, 
19 Jan 2012 14:27:46 +0100,
  Simon Arnaud <ruby-forum-incoming@andreas-s.net> 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
Posted by Simon Arnaud (sarnaud)
on 2012-01-19 15:17
Kouhei Sutou 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
Posted by Kouhei Sutou (Guest)
on 2012-01-20 11:16
(Received via mailing list)
Hi,

In <7029a41c297b963cf3ff685da3d0b96a@ruby-forum.com>
  "Re: [ruby-gnome2-devel-en] Capture exception in a rescue block" on 
Thu, 19 Jan 2012 15:17:03 +0100,
  Simon Arnaud <ruby-forum-incoming@andreas-s.net> 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
Posted by Simon Arnaud (sarnaud)
on 2012-01-20 14:38
Attachment: gtk_backtrace_raise.rb (364 Bytes)
Attachment: gtk_backtrace_raise_exception.rb (374 Bytes)
Attachment: gtk_backtrace_empty.rb (354 Bytes)
Kouhei Sutou wrote in post #1041758:
> Hi,
>
> In <7029a41c297b963cf3ff685da3d0b96a@ruby-forum.com>
>   "Re: [ruby-gnome2-devel-en] Capture exception in a rescue block" on
> Thu, 19 Jan 2012 15:17:03 +0100,
>   Simon Arnaud <ruby-forum-incoming@andreas-s.net> 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
Posted by Kouhei Sutou (Guest)
on 2012-01-22 04:47
(Received via mailing list)
Hi,

In <b4d973333367c33820a4d48a740947ba@ruby-forum.com>
  "Re: [ruby-gnome2-devel-en] Capture exception in a rescue block" on 
Fri, 20 Jan 2012 14:38:27 +0100,
  Simon Arnaud <ruby-forum-incoming@andreas-s.net> 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
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.