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