Re: Exceptions while running a dialog box




On Jun 8, 2006, at 1:45 AM, Ari Jolma wrote:

I've got dialog boxes, with which the user may interact and which are modal. In some cases the user requests something that does not work out and that causes an exception. In my current implementation this is a problem since the dialog box stays visible but not working. There are two possibilities: close the dialog window (from the X at up right) or "open" the dialog from the main window again. The first one destroys the dialog and it is not usable any more (the dialog is created from glade XML and used like this: show, run, hide) and seem to cause problems when the program is closed.

Somehow you've managed to structure your code such that when an exception is thrown, it jumps past the line where you destroy the dialog. Since toplevel windows are actually owned by gtk+, and dialogs are toplevel windows, just letting the dialog's perl variable go out of scope isn't sufficient to destroy it. And, since the dialog is a glade dialog, you probably don't want it to be destroyed, anyway, only hidden.


What would be the best solution for this kind of case? All I can think is to get rid of or avoid croaks.

In general, the approach is to enclose the pieces that can die in an eval block. That is, eval with a block, not with a code string.

I'm going to guess your code looks something like this:

        sub do_the_dialog_schtick {
            $dialog = $xml->get_widget ('my-super-awesome-dialog');
            if ('ok'  eq $dialog->run) {
                my $value = $xml->get_widget ('dialog-entry');
                do_something($value);  # this guy croaks somewhere
            }
            $dialog->hide;
        }

And you're throwing an exception inside do_something(), which jumps all the way out of do_the_dialog_schtick(), skipping the $dialog- >hide line. The fix goes like this:

        sub do_the_dialog_schtick {
            $dialog = $xml->get_widget ('my-super-awesome-dialog');
            if ('ok'  eq $dialog->run) {
                my $value = $xml->get_widget ('dialog-entry');
                eval {
                    do_something($value);
                };
                if ($@) {
                    # there was an exception.  do something about it.
                    tell_user_what_he_did_wrong ($@);
                }
            }
            $dialog->hide;
        }



--
Jolt is my co-pilot.
  -- Slogan on a giant paper airplane hung in Lobby 7 at MIT.
     http://hacks.mit.edu/




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]