Cross-Platform GUI Programming with wxWidgets
|
wxWidgets was created long before exceptions were introduced in C++ and has had to work with compilers with varying levels of exception support, so exceptions are not used throughout the framework. However, it safe to use exceptions in application code, and the library tries to help you. There are several choices for using exceptions in wxWidgets programs. First, you can avoid using them at all. The library doesn't throw any exceptions by itself, so you don't have to worry about exceptions at all unless your own code throws them. This is the simplest solution, but it may be not the best one to deal with all possible errors. Another strategy is to use exceptions only to signal truly fatal errors. In this case, you probably don't expect to recover from them, and the default behaviorto simply terminate the programmay be appropriate. If it is not, you can override OnUnhandledException in your wxApp-derived class to perform any cleanup tasks. Note that any information about the exact exception type is lost when this function is called, so if you need this information, you should override OnRun and add a TRy/catch clause around the call of the base class version. This would enable you to catch any exceptions generated during the execution of the main event loop. To deal with exceptions that may arise during the program startup and shutdown, you should insert TRy/catch clauses in OnInit and OnExit. Finally, you might also want the application to continue running even when certain exceptions occur. If all your exceptions can happen only in the event handlers of a single class (or only in the classes derived from it), you can centralize your exception handling code in the ProcessEvent method of this class. If this is impractical, you might also consider overriding the wxApp::HandleEvent, which allows you to handle all the exceptions thrown by any event handler. To enable exception support in wxWidgets, you need to build it with wxUSE_EXCEPTIONS set to 1. This should be the case by default, but if it isn't, you should edit include/wx/msw/setup.h under Windows or run configure with --enable-exceptions under Unix. If you do not plan to use exceptions, setting this flag to 0 or using --disable-exceptions results in a leaner and slightly faster library. Also, if you have Visual C++ and want a user-defined wxApp::OnFatalException function to be called instead of a GPF occurring, set wxUSE_ON_FATAL_EXCEPTION to 1 in your setup.h. Conversely, if you would rather be dropped into the debugger when an error in your program occurs, set this to 0. Please look at samples/except for examples of using exceptions with wxWidgets. |
|