2003-09-17 19:25:22 -04:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
%% Name: texcept.tex
|
2004-05-04 04:27:20 -04:00
|
|
|
%% Purpose: C++ exceptions and wxWidgets overview
|
2003-09-17 19:25:22 -04:00
|
|
|
%% Author: Vadim Zeitlin
|
|
|
|
%% Modified by:
|
|
|
|
%% Created: 17.09.03
|
|
|
|
%% RCS-ID: $Id$
|
|
|
|
%% Copyright: (c) 2003 Vadim Zeitlin
|
2004-05-04 04:27:20 -04:00
|
|
|
%% License: wxWidgets license
|
2003-09-17 19:25:22 -04:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
|
|
|
\section{C++ exceptions overview}\label{exceptionsoverview}
|
|
|
|
|
2004-09-20 07:21:04 -04:00
|
|
|
\subsection{Introduction}\label{exceptionintroduction}
|
2003-09-17 19:25:22 -04:00
|
|
|
|
2004-05-04 04:27:20 -04:00
|
|
|
wxWidgets had been started long before the exceptions were introduced in C++ so
|
2004-10-22 15:15:35 -04:00
|
|
|
it is not very surprising that it is not built around using them as some more
|
2003-09-17 19:25:22 -04:00
|
|
|
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
|
2004-05-04 04:27:20 -04:00
|
|
|
wxWidgets, even using the exceptions in the user code was dangerous because the
|
2003-09-17 19:25:22 -04:00
|
|
|
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.
|
|
|
|
|
2004-05-04 04:27:20 -04:00
|
|
|
Starting from the version 2.5.1 wxWidgets becomes more exception-friendly. It
|
2003-09-17 19:25:22 -04:00
|
|
|
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.
|
|
|
|
|
|
|
|
|
2004-09-20 07:21:04 -04:00
|
|
|
\subsection{Strategies for exceptions handling}\label{exceptionstrategies}
|
2003-09-17 19:25:22 -04:00
|
|
|
|
2004-05-04 04:27:20 -04:00
|
|
|
There are several choice for using the exceptions in wxWidgets programs. First
|
2003-09-17 19:25:22 -04:00
|
|
|
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 \helpref{OnUnhandledException()}{wxapponunhandledexception}
|
|
|
|
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 \helpref{OnRun()}{wxapponrun} 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
|
2003-09-20 12:20:43 -04:00
|
|
|
\helpref{OnInit()}{wxapponinit} and/or \helpref{OnExit()}{wxapponexit} as well.
|
2003-09-17 19:25:22 -04:00
|
|
|
|
|
|
|
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 \helpref{ProcessEvent}{wxevthandlerprocessevent}
|
|
|
|
method of this class. If this is impractical, you may also consider overriding
|
|
|
|
the \helpref{wxApp::HandleEvent()}{wxapphandleevent} which allows you to handle
|
|
|
|
all the exceptions thrown by any event handler.
|
|
|
|
|
|
|
|
|
2004-09-20 07:21:04 -04:00
|
|
|
\subsection{Technicalities}\label{exceptionstechnicalities}
|
2003-09-17 19:25:22 -04:00
|
|
|
|
|
|
|
To use any kind of exception support in the library you need to build it with
|
|
|
|
\texttt{wxUSE\_EXCEPTIONS} set to $1$. This should be the case by default but
|
|
|
|
if it isn't, you should edit the \texttt{include/wx/msw/setup.h} file under
|
|
|
|
Windows or run \texttt{configure} with \texttt{--enable-exceptions} argument
|
|
|
|
under Unix.
|
|
|
|
|
|
|
|
On the other hand, if you do \emph{not} plan to use exceptions, setting this
|
|
|
|
flag to $0$ or using \texttt{--disable-exceptions} could result in a leaner and
|
|
|
|
slightly faster library.
|
|
|
|
|
|
|
|
As for any other library feature, there is a \helpref{sample}{sampleexcept}
|
|
|
|
showing how to use it. Please look at its sources for further information.
|
|
|
|
|
|
|
|
|