c++ exceptions overview
introduction
introductionwxwidgets had been started long before the exceptions were introduced in c++ so it is not very surprising that it is not built around using them as some more modern c++ libraries are. for instance, the library doesn't throw exceptions to signal about the errors. moreover, up to (and including) the version 2.4 of wxwidgets, even using the exceptions in the user code was dangerous because the library code wasn't exception-safe and so an exception propagating through it could result in memory and/or resource leaks, and also not very convenient. starting from the version 2.5.1 wxwidgets becomes more exception-friendly. it still doesn't use the exceptions by itself but it should be now safe to use the exceptions in the user code and the library tries to help you with this. please note that making the library exception-safe is still work in progress.
strategies for exceptions handlingthere are several choice for using the exceptions in wxwidgets programs. first of all, you may not use them at all. as stated above, the library doesn't throw any exceptions by itself and so you don't have to worry about exceptions at all unless your own code throws them. this is, of course, the simplest solution but 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 behaviour -- to simply terminate the program -- may be appropriate. if it is not, you may override onunhandledexception() in your wxapp-derived class to perform any clean up tasks. note, however, that any information about the exact exception type is lost when this function is called, so if you need you should override onrun() and add a try/catch clause around the call of the base class version. this would allow you to catch any exceptions generated during the execution of the main event loop. to deal with the exceptions which may arise during the program startup and/or shutdown you should insert try/catch clauses in oninit() and/or onexit() as well. finally, you may also want to continue running even when certain exceptions occur. if all of your exceptions may happen only in the event handlers of a single class (or only in the classes derived from it), you may centralize your exception handling code in processevent method of this class. if this is impractical, you may also consider overriding the wxapp::handleevent() which allows you to handle all the exceptions thrown by any event handler.
technicalitiesto use any kind of exception support in the library 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 the include/wx/msw/setup.h file under windows or run configure with --enable-exceptions argument under unix. on the other hand, if you do not plan to use exceptions, setting this flag to 0 or using --disable-exceptions could result in a leaner and slightly faster library. as for any other library feature, there is a sample showing how to use it. please look at its sources for further information.
|