try/catch Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
leiradel
Earned a small fee
Earned a small fee
Posts: 18
Joined: Wed Jan 23, 2008 6:42 pm

try/catch

Post by leiradel »

Hi All,

I'm developing a wxWidgets application and am using a scripting language to implement some parts of it, mainly event handling.

The scripts that handle the events can have runtime errors, and those cause the scripting engine to throw exceptions.

The problem is that whenever an exception is thrown from inside an event handle, I can't catch it and the application aborts. While the scripts won't have runtime errors when the application is finished, it's a bit annoying having it aborted from time to time during development, specially because the exception shows me the location of the error in the script along with a stack trace.

How can I overwrite wxWidget's event handling system in order to catch all exceptions in a way that it doesn't change the way it works? Ideally, this would be some method I can override:

Code: Select all

class MyApp: public wxApp {
  public:
    Application() {}
    virtual bool OnInit();
    virtual void EventHandler(wxEvent& evt);
};

void MyApp::EventHandler(wxEvent& evt) {
  try {
    // handle the event just like if
    // I've never overridden this method
    wxApp::EventHandler(evt);
  } catch (wxString error) {
    // show error message and go on
    // as if it never happened
  }
}
Again: I don't want to change the way events are processed in my application. I just want to catch any exceptions the event handlers may throw.

Thanks in advance,

Andre
tan
wxWorld Domination!
wxWorld Domination!
Posts: 1471
Joined: Tue Nov 14, 2006 7:58 am
Location: Saint-Petersburg, Russia

Post by tan »

OS: Windows XP Pro
Compiler: MSVC++ 7.1
wxWidgets: 2.8.10
leiradel
Earned a small fee
Earned a small fee
Posts: 18
Joined: Wed Jan 23, 2008 6:42 pm

Post by leiradel »

Ha!, I think I should have checked wxApp just like I suggested in the first message!

Thanks for the link, it's better than what I had before (loosing the exceptions) but the abort/retry/ignore dialog box appears no matter what (I'm developing on Windows.)

My code now is:

Code: Select all

class MyApp: public wxApp
{
	public:
		MyApp() { }
		virtual bool OnInit();
		virtual int OnRun();

	private:
		MainFrame *frame;

	DECLARE_NO_COPY_CLASS(MyApp)
};

IMPLEMENT_APP(Application)

bool
MyApp::OnInit()
{
	frame = new MainFrame();
	frame->Show(true);
	return true;
}

int
MyApp::OnRun()
{
	again:
		try
		{
			return wxApp::OnRun();
		}
		catch (wxString error)
		{
			frame->ShowMessage(wxT("Error"), UIManager::OkButton | UIManager::ErrorIcon, error);
			goto again;
		}
}
The UIManager is just a bunch of abstract methods to show info to the user, and MainFrame implements them.

With the code as shown, the abort/retry/ignore dialog appears. If I click on abort, the program shows the message and continues. If I click on retry, the program exits without showing the message. And if I click on ignore, the exception is eaten (no message is shown) and program continues.

I've tried this with other methods:

OnExceptionInMainLoop(): If I return true from it, the exception is eaten and the program continues. If I return false, the program exits without showing the message.

OnUnhandledException(): The program behaves just like this method wasn't overridden.

So it seems the only way I can see the exception is by overriding only the OnRun method and clicking on the abort button whenever an exception is thrown. So my question now is: is there a way to avoid the abort/retry/ignore dialog?

AFAIK, the abort/retry/ignore dialog can be written by the user by overriding OnRun:

. abort: the application exits by exiting OnRun
. retry: the exception is rethrown
. ignore: wxApp::OnRun is called again

So in my opinion it should be removed from wxWidgets.

Thanks again,

Andre
leiradel
Earned a small fee
Earned a small fee
Posts: 18
Joined: Wed Jan 23, 2008 6:42 pm

Post by leiradel »

Just for the records, I've tried overriding wxEvtHandler::ProcessEvent with no luck. Exceptions seem to never reach this method.

Cheers,

Andre
Frank
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 211
Joined: Sat Jan 01, 2005 6:19 pm

Post by Frank »

Hi,

wx calls the Function wxApp::OnExceptionInMainLoop() when it catches an exception (I think you must set some exceptions-blah in the setup.h, don't remember).

Because there is no information in OnExceptionInMainLoop(), it is of course useless. So I just rethrow it:

Code: Select all

bool MyApp::OnExceptionInMainLoop ()
{
   throw;
}
Now, I can catch it in OnRun(), with a normal try/catch, so that I have the type and message of the exception.

Greetings
Frank
leiradel
Earned a small fee
Earned a small fee
Posts: 18
Joined: Wed Jan 23, 2008 6:42 pm

Post by leiradel »

Thanks, that solved the problem. I can now catch my exceptions and the abort/retry/ignore dialog doesn't appear anymore.

Cheers,

Andre
Post Reply