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