# FXRuby default error handler

**URL:** <https://www.ruby-forum.com/t/fxruby-default-error-handler/116642>\
**Category:** FXRuby\
**Created:** [October 8, 2007, 11:53am UTC](https://www.ruby-forum.com/t/fxruby-default-error-handler/116642 "2007-10-08T11:53:15Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![philpraxis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/77aa72/32.png) [@philpraxis](https://www.ruby-forum.com/u/philpraxis)\
**Post date:** [October 8, 2007, 11:53am UTC](https://www.ruby-forum.com/t/fxruby-default-error-handler/116642/1 "2007-10-08T11:53:15Z")

</div>

Hi,

I’m using FXRuby for building a database front-end. I have quite a lot  
of classes now, made up of multiple methods each. Most of the time I use  
rescue blocks in case an exception is raised by the framemork. But doing  
that in every method of every class is really tedious.

If an exception is raised from a method without a rescue block, the  
problem is that my application quits, since the application “run” method  
stops.

begin

```
$app = MyApp.new
$app.create
$app.run

```

rescue Exception =\> e

```
DbError.show("", e)

```

end

I was wondering if we could define some sort of “default error handler”  
for errors raised from the framework, in order to avoid both the  
application crash, and the “one rescue per method” solution…

Thanks!

Philippe L.

---

<div class="post-metadata">

**Author:** ![philpraxis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/77aa72/32.png) [@philpraxis](https://www.ruby-forum.com/u/philpraxis)\
**Post date:** [October 8, 2007, 9:44pm UTC](https://www.ruby-forum.com/t/fxruby-default-error-handler/116642/2 "2007-10-08T21:44:15Z")

</div>

Philippe L. wrote:

> end
> 
> I was wondering if we could define some sort of “default error handler”  
> for errors raised from the framework, in order to avoid both the  
> application crash, and the “one rescue per method” solution…

That’s a good question.

I usually just try to make “one rescue per method” as painless as  
possible, by writing wrapper methods. Still, that’s “one wrapper per  
method”, which is only a slight improvement.

---

<div class="post-metadata">

**Author:** ![philpraxis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/77aa72/32.png) [@philpraxis](https://www.ruby-forum.com/u/philpraxis)\
**Post date:** [October 8, 2007, 10:56pm UTC](https://www.ruby-forum.com/t/fxruby-default-error-handler/116642/3 "2007-10-08T22:56:35Z")

</div>

On 10/8/07, Joel VanderWerf [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > stops.  
> > DbError.show(“”, e)  
> > I usually just try to make “one rescue per method” as painless as  
> > possible, by writing wrapper methods. Still, that’s “one wrapper per  
> > method”, which is only a slight improvement.

Try to centralize access to the DB. Channel all method calls to the DB  
through one dedicated method that handles db errors. something like  
this could do the job:

class DB  
def safe\_call( method, \*args, &block)  
self.send( method, \*args, &block)  
rescue DBError

# all db errorhandling here

end  
end

there might be even better solutions

– meinrad

---

<div class="post-metadata">

**Author:** ![philpraxis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/77aa72/32.png) [@philpraxis](https://www.ruby-forum.com/u/philpraxis)\
**Post date:** [October 9, 2007, 4:51pm UTC](https://www.ruby-forum.com/t/fxruby-default-error-handler/116642/4 "2007-10-09T16:51:57Z")

</div>

removed\_email\_address@domain.invalid wrote:

> > problem is that my application quits, since the application “run”
> 
> That’s a good question.
> 
> I usually just try to make “one rescue per method” as painless as  
> possible, by writing wrapper methods. Still, that’s “one wrapper per  
> method”, which is only a slight improvement.

Hi everyone,

Lyle, do you think it would be possible to add this feature to the FXApp  
class: the ability to register some sort of “rescue handler”, that can  
intercept in the “run” method the exceptions that would otherwise crash  
the application? For compatitility, the default handler could simply  
raise the exception again, which is what the framework is doing now.

This would be really useful.

Regards,

Philippe L.

---

<div class="post-metadata">

**Author:** ![philpraxis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/77aa72/32.png) [@philpraxis](https://www.ruby-forum.com/u/philpraxis)\
**Post date:** [October 9, 2007, 6:27pm UTC](https://www.ruby-forum.com/t/fxruby-default-error-handler/116642/5 "2007-10-09T18:27:10Z")

</div>

On Oct 9, 2007, at 9:51 AM, Philippe L. wrote:

> Lyle, do you think it would be possible to add this feature to the  
> FXApp  
> class: the ability to register some sort of “rescue handler”, that can  
> intercept in the “run” method the exceptions that would otherwise  
> crash  
> the application? For compatitility, the default handler could simply  
> raise the exception again, which is what the framework is doing now.

If you will file a bug report (or feature request) about it, I’ll  
consider it. I know that it’s sometimes difficult to work around  
FOX’s error handling mechanisms, since Jeroen just calls exit() on  
most errors; but particular circumstance may not be affected by that.

---

<div class="post-metadata">

**Author:** ![philpraxis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/77aa72/32.png) [@philpraxis](https://www.ruby-forum.com/u/philpraxis)\
**Post date:** [October 10, 2007, 5:33pm UTC](https://www.ruby-forum.com/t/fxruby-default-error-handler/116642/6 "2007-10-10T17:33:09Z")

</div>

removed\_email\_address@domain.invalid wrote:

> > If you will file a bug report (or feature request) about it, I’ll
> 
> begin  
> app.run  
> rescue =\> ex  
> if … # ex can be retried  
> retry  
> end  
> end  
> app.exit

Hi Joel,

This is exactly the kind of thing I was takling about, yes, and it works  
pretty well, but not 100%, apparently. I’m not sure an FXApp can be  
“re-run” that way, without taking risks. Is that true Joeroen?

In my tests, I have noticed for example that an FXTabBook, after an  
exception caught by your technique, has got “zombie” pages in it: the  
place is still allocated for a page tab, but no page tab is visible at  
all.

I also tried that, but without success:

```
begin
  app.repaint
  app.forceRefresh
  app.flush
  app.run
rescue Exception => e
  ---> show error to the user
  retry
end
app.exit

```

Philippe

---

<div class="post-metadata">

**Author:** ![philpraxis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/77aa72/32.png) [@philpraxis](https://www.ruby-forum.com/u/philpraxis)\
**Post date:** [October 10, 2007, 11:14pm UTC](https://www.ruby-forum.com/t/fxruby-default-error-handler/116642/7 "2007-10-10T23:14:16Z")

</div>

Philippe L. wrote:

> removed\_email\_address@domain.invalid wrote:  
> ,
> 
> Hi Joel,
> 
> This is exactly the kind of thing I was takling about, yes, and it works  
> pretty well, but not 100%, apparently. I’m not sure an FXApp can be  
> “re-run” that way, without taking risks. Is that true Joeroen?
> 
> In my tests, I have noticed for example that an FXTabBook, after an  
> exception caught by your technique, has got “zombie” pages in it: the  
> place is still allocated for a page tab, but no page tab is visible at  
> all.

The reason I thought it _should_ work is that FXApp::run() and  
FXApp::runWhileEvents() are doing basically the same thing, with  
slightly different logic. So if it is safe to unwind from runWhileEvents  
and then re-enter the event loop, then it should be safe to do so with  
run as well.

---

<div class="post-metadata">

**Author:** ![philpraxis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/77aa72/32.png) [@philpraxis](https://www.ruby-forum.com/u/philpraxis)\
**Post date:** [October 9, 2007, 7:37pm UTC](https://www.ruby-forum.com/t/fxruby-default-error-handler/116642/8 "2007-10-09T19:37:05Z")

</div>

Lyle J. wrote:

> If you will file a bug report (or feature request) about it, I’ll  
> consider it. I know that it’s sometimes difficult to work around  
> FOX’s error handling mechanisms, since Jeroen just calls exit() on  
> most errors; but particular circumstance may not be affected by that.

I don’t think Philippe is talking about those kinds of situations (that  
cause Fox to call exit()), but rather situations where ruby callback  
raises a ruby exception, and it propagates outside of the #run call.

Is there any reason why the following code won’t work?

begin  
app.run  
rescue =\> ex  
if … # ex can be retried  
retry  
end  
end  
app.exit

---

<div class="post-metadata">

**Author:** ![philpraxis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/77aa72/32.png) [@philpraxis](https://www.ruby-forum.com/u/philpraxis)\
**Post date:** [October 11, 2007, 9:48am UTC](https://www.ruby-forum.com/t/fxruby-default-error-handler/116642/9 "2007-10-11T09:48:37Z")

</div>

On 10/10/07, Joel VanderWerf [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > ```
> > > end
> > > 
> > > ```
> > 
> > exception caught by your technique, has got “zombie” pages in it: the  
> > place is still allocated for a page tab, but no page tab is visible at  
> > all.
> 
> The reason I thought it _should_ work is that FXApp::run() and  
> FXApp::runWhileEvents() are doing basically the same thing, with  
> slightly different logic. So if it is safe to unwind from runWhileEvents  
> and then re-enter the event loop, then it should be safe to do so with  
> run as well.

… if there weren’t all those nasty side effects 😛

---

<div class="post-metadata">

**Author:** ![philpraxis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/77aa72/32.png) [@philpraxis](https://www.ruby-forum.com/u/philpraxis)\
**Post date:** [October 11, 2007, 10:57am UTC](https://www.ruby-forum.com/t/fxruby-default-error-handler/116642/10 "2007-10-11T10:57:46Z")

</div>

removed\_email\_address@domain.invalid wrote:

> > 
> 
> The reason I thought it _should_ work is that FXApp::run() and  
> FXApp::runWhileEvents() are doing basically the same thing, with  
> slightly different logic. So if it is safe to unwind from  
> runWhileEvents and then re-enter the event loop, then it should be  
> safe to do so with run as well.

OK, I managed to reduce my problem to the following test program: as  
soon as you try to add a “buggy tab”, the GUI starts to mess up. The  
result, you will see, is different if you start by adding a buggy tab,  
or if you start by adding a normal tab.

Maybe I need to call some kind of “refresh” after the exception is  
raised?

* * *

#!/usr/bin/ruby

require ‘fox16’

include Fox

class MyFXTabItem \< FXTabItem

end

class MyBuggyFXTabItem \< FXTabItem  
def initialize(text, icon=nil, data=nil)  
super  
raise Exception.new  
end  
end

class MyWindow \< FXMainWindow

def initialize(app)

```
super(app, "Window", nil, nil, DECOR_ALL, 0, 0, 600, 350)

# Menu bar stretched along the top of the main window
menubar = FXMenuBar.new(self, LAYOUT_SIDE_TOP|LAYOUT_FILL_X)

# File menu
filemenu = FXMenuPane.new(self)
FXMenuTitle.new(menubar, "&File", nil, filemenu)
FXMenuCommand.new(filemenu, "&Quit\tCtl-Q\tQuit the application",
    nil, app, FXApp::ID_QUIT)

# Content
contents = FXVerticalFrame.new(self, LAYOUT_FILL_X|LAYOUT_FILL_Y)

# Tabs
@tabbook = FXTabBook.new(contents,:opts =>

```

LAYOUT\_FILL\_X|LAYOUT\_FILL\_Y|LAYOUT\_RIGHT)

```
FXButton.new(contents, "add tab").connect(SEL_COMMAND) do |sender,

```

selector, data|  
# First item is a list  
tab = MyFXTabItem.new(@tabbook, “&Simple List”, nil)  
frame = FXHorizontalFrame.new(@tabbook,  
FRAME\_THICK|FRAME\_RAISED)  
simplelist = FXList.new(frame, :opts =\>  
LIST\_EXTENDEDSELECT|LAYOUT\_FILL\_X|LAYOUT\_FILL\_Y)  
simplelist.appendItem(“First Entry”)  
simplelist.appendItem(“Second Entry”)  
simplelist.appendItem(“Third Entry”)  
simplelist.appendItem(“Fourth Entry”)

```
  frame.create
  tab.create
  tab.show

```

end

FXButton.new(contents, “add buggy tab”).connect(SEL\_COMMAND) do  
|sender, selector, data|  
# Second item is a file list  
tab = MyBuggyFXTabItem.new(@tabbook, “F&ile List”, nil)  
frame = FXHorizontalFrame.new(@tabbook,  
FRAME\_THICK|FRAME\_RAISED)  
filelist = FXFileList.new(frame, :opts =\>  
ICONLIST\_EXTENDEDSELECT|LAYOUT\_FILL\_X|LAYOUT\_FILL\_Y)

```
  frame.create
  tab.create
  tab.show

```

end

end

def create  
super  
show(PLACEMENT\_SCREEN)  
end

end

if **FILE** == $0

$app = FXApp.new(“Attik System”, “FXRuby Test”)  
MyWindow.new($app)  
$app.create

begin  
$app.run  
rescue Exception =\> e  
FXMessageBox.information($app, MBOX\_OK, “Error”, “An exception was  
raised”)  
retry  
end  
end
