]> git.saurik.com Git - wxWidgets.git/blame_incremental - docs/doxygen/overviews/exceptions.h
Move wx/msw/gccpriv.h inclusion back to wx/platform.h from wx/compiler.h.
[wxWidgets.git] / docs / doxygen / overviews / exceptions.h
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: exceptions.h
3// Purpose: topic overview
4// Author: wxWidgets team
5// RCS-ID: $Id$
6// Licence: wxWindows licence
7/////////////////////////////////////////////////////////////////////////////
8
9/**
10
11@page overview_exceptions C++ Exceptions
12
13@tableofcontents
14
15wxWidgets had been started long before the exceptions were introduced in C++ so
16it is not very surprising that it is not built around using them as some more
17modern C++ libraries are. For instance, the library doesn't throw exceptions to
18signal about the errors. Moreover, up to (and including) the version 2.4 of
19wxWidgets, even using the exceptions in the user code was dangerous because the
20library code wasn't exception-safe and so an exception propagating through it
21could result in memory and/or resource leaks, and also not very convenient.
22
23However the recent wxWidgets versions are exception-friendly. This means that
24while the library still doesn't use the exceptions by itself, it should be now
25safe to use the exceptions in the user code and the library tries to help you
26with this.
27
28
29
30@section overview_exceptions_strategies Strategies for Exception Handling
31
32There are several choice for using the exceptions in wxWidgets programs. First
33of all, you may not use them at all. As stated above, the library doesn't throw
34any exceptions by itself and so you don't have to worry about exceptions at all
35unless your own code throws them. This is, of course, the simplest solution but
36may be not the best one to deal with all possible errors.
37
38The next simplest strategy is to only use exceptions inside non-GUI code, i.e.
39never let unhandled exceptions escape the event handler in which it happened.
40In this case using exceptions in wxWidgets programs is not different from using
41them in any other C++ program.
42
43Things get more interesting if you decide to let (at least some) exceptions
44escape from the event handler in which they occurred. Such exceptions will be
45caught by wxWidgets and the special wxApp::OnExceptionInMainLoop() method will
46be called from the @c catch clause. This allows you to decide in a single place
47what to do about such exceptions: you may want to handle the exception somehow
48or terminate the program. In this sense, OnExceptionInMainLoop() is equivalent
49to putting a @c try/catch block around the entire @c main() function body in
50the traditional console programs. However notice that, as its name indicates,
51this method won't help you with the exceptions thrown before the main loop is
52started or after it is over, so you may still want to have @c try/catch in your
53overridden wxApp::OnInit() and wxApp::OnExit() methods too, otherwise
54wxApp::OnUnhandledException() will be called.
55
56Finally, notice that even if you decide to not let any exceptions escape in
57this way, this still may happen unexpectedly in a program using exceptions as a
58result of a bug. So consider always overriding OnExceptionInMainLoop() in your
59wxApp-derived class if you use exceptions in your program, whether you expect
60it to be called or not. In the latter case you may simple re-throw the
61exception and let it bubble up to OnUnhandledException() as well.
62
63To summarize, when you use exceptions in your code, you may handle them in the
64following places, in order of priority:
65 -# In a @c try/catch block inside an event handler.
66 -# In wxApp::OnExceptionInMainLoop().
67 -# In wxApp::OnUnhandledException().
68
69In the first two cases you may decide whether you want to handle the exception
70and continue execution or to exit the program. In the last one the program is
71about to exit already so you can just try to save any unsaved data and notify
72the user about the problem (while being careful not to throw any more
73exceptions as otherwise @c std::terminate() will be called).
74
75
76@section overview_exceptions_tech Technicalities
77
78To use any kind of exception support in the library you need to build it
79with @c wxUSE_EXCEPTIONS set to 1. It is turned on by default but you may
80wish to check @c include/wx/msw/setup.h file under Windows or run @c configure
81with explicit @c --enable-exceptions argument under Unix.
82
83On the other hand, if you do not plan to use exceptions, setting this
84flag to 0 or using @c --disable-exceptions could result in a leaner and
85slightly faster library.
86
87As for any other library feature, there is a sample (@c except)
88showing how to use it. Please look at its sources for further information.
89
90*/