]>
Commit | Line | Data |
---|---|---|
556b8c1a | 1 | ///////////////////////////////////////////////////////////////////////////// |
f78e4942 VZ |
2 | // Name: samples/except/except.cpp |
3 | // Purpose: shows how C++ exceptions can be used in wxWidgets | |
4 | // Author: Vadim Zeitlin | |
556b8c1a | 5 | // Modified by: |
f78e4942 | 6 | // Created: 2003-09-17 |
556b8c1a | 7 | // RCS-ID: $Id$ |
f78e4942 | 8 | // Copyright: (c) 2003-2005 Vadim Zeitlin |
556b8c1a VZ |
9 | // Licence: wxWindows licence |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | // For compilers that support precompilation, includes "wx/wx.h". | |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
f78e4942 VZ |
27 | #if !wxUSE_EXCEPTIONS |
28 | #error "This sample only works with wxUSE_EXCEPTIONS == 1" | |
29 | #endif // !wxUSE_EXCEPTIONS | |
30 | ||
556b8c1a | 31 | // for all others, include the necessary headers (this file is usually all you |
be5a51fb | 32 | // need because it includes almost all "standard" wxWidgets headers) |
556b8c1a | 33 | #ifndef WX_PRECOMP |
532c7736 VZ |
34 | #include "wx/log.h" |
35 | ||
556b8c1a VZ |
36 | #include "wx/app.h" |
37 | #include "wx/frame.h" | |
38 | #include "wx/dialog.h" | |
532c7736 | 39 | #include "wx/menu.h" |
556b8c1a VZ |
40 | |
41 | #include "wx/button.h" | |
42 | #include "wx/sizer.h" | |
43 | ||
30deac1f WS |
44 | #include "wx/utils.h" |
45 | #include "wx/msgdlg.h" | |
60c474a0 | 46 | #include "wx/icon.h" |
556b8c1a VZ |
47 | #endif |
48 | ||
49 | // ---------------------------------------------------------------------------- | |
50 | // resources | |
51 | // ---------------------------------------------------------------------------- | |
52 | ||
53 | // the application icon (under Windows and OS/2 it is in resources) | |
54 | #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__) | |
55 | #include "../sample.xpm" | |
56 | #endif | |
57 | ||
cc9a3957 VZ |
58 | // ---------------------------------------------------------------------------- |
59 | // private functions | |
60 | // ---------------------------------------------------------------------------- | |
61 | ||
62 | static void DoCrash() | |
63 | { | |
64 | char *p = 0; | |
65 | strcpy(p, "Let's crash"); | |
66 | } | |
67 | ||
556b8c1a VZ |
68 | // ---------------------------------------------------------------------------- |
69 | // private classes | |
70 | // ---------------------------------------------------------------------------- | |
71 | ||
72 | // Define a new application type, each program should derive a class from wxApp | |
73 | class MyApp : public wxApp | |
74 | { | |
75 | public: | |
76 | // override base class virtuals | |
77 | // ---------------------------- | |
78 | ||
79 | // program startup | |
80 | virtual bool OnInit(); | |
81 | ||
3103e8a9 | 82 | // 2nd-level exception handling: we get all the exceptions occurring in any |
556b8c1a | 83 | // event handler here |
cc9a3957 | 84 | virtual bool OnExceptionInMainLoop(); |
556b8c1a VZ |
85 | |
86 | // 3rd, and final, level exception handling: whenever an unhandled | |
87 | // exception is caught, this function is called | |
88 | virtual void OnUnhandledException(); | |
cc9a3957 VZ |
89 | |
90 | // and now for something different: this function is called in case of a | |
91 | // crash (e.g. dereferencing null pointer, division by 0, ...) | |
92 | virtual void OnFatalException(); | |
3eeb7e10 VZ |
93 | |
94 | #ifdef __WXDEBUG__ | |
95 | // in debug mode, you can override this function to do something different | |
96 | // (e.g. log the assert to file) whenever an assertion fails | |
97 | virtual void OnAssert(const wxChar *file, | |
98 | int line, | |
99 | const wxChar *cond, | |
100 | const wxChar *msg); | |
101 | #endif // __WXDEBUG__ | |
556b8c1a VZ |
102 | }; |
103 | ||
104 | // Define a new frame type: this is going to be our main frame | |
105 | class MyFrame : public wxFrame | |
106 | { | |
107 | public: | |
108 | // ctor(s) | |
cc9a3957 | 109 | MyFrame(); |
556b8c1a VZ |
110 | |
111 | // event handlers (these functions should _not_ be virtual) | |
112 | void OnQuit(wxCommandEvent& event); | |
113 | void OnAbout(wxCommandEvent& event); | |
114 | void OnDialog(wxCommandEvent& event); | |
cc9a3957 VZ |
115 | |
116 | void OnThrowInt(wxCommandEvent& event); | |
556b8c1a | 117 | void OnThrowString(wxCommandEvent& event); |
cc9a3957 VZ |
118 | void OnThrowObject(wxCommandEvent& event); |
119 | void OnThrowUnhandled(wxCommandEvent& event); | |
120 | ||
121 | void OnCrash(wxCommandEvent& event); | |
b6b375a9 | 122 | #if wxUSE_ON_FATAL_EXCEPTION |
cc9a3957 | 123 | void OnHandleCrash(wxCommandEvent& event); |
b6b375a9 | 124 | #endif |
556b8c1a VZ |
125 | |
126 | // 1st-level exception handling: we overload ProcessEvent() to be able to | |
127 | // catch exceptions which occur in MyFrame methods here | |
128 | virtual bool ProcessEvent(wxEvent& event); | |
129 | ||
3eeb7e10 VZ |
130 | #ifdef __WXDEBUG__ |
131 | // show how an assert failure message box looks like | |
132 | void OnShowAssert(wxCommandEvent& event); | |
133 | #endif // __WXDEBUG__ | |
134 | ||
556b8c1a | 135 | private: |
be5a51fb | 136 | // any class wishing to process wxWidgets events must use this macro |
556b8c1a VZ |
137 | DECLARE_EVENT_TABLE() |
138 | }; | |
139 | ||
140 | // A simple dialog which has only some buttons to throw exceptions | |
141 | class MyDialog : public wxDialog | |
142 | { | |
143 | public: | |
144 | MyDialog(wxFrame *parent); | |
145 | ||
146 | // event handlers | |
147 | void OnThrowInt(wxCommandEvent& event); | |
148 | void OnThrowObject(wxCommandEvent& event); | |
f78e4942 | 149 | void OnCrash(wxCommandEvent& event); |
556b8c1a VZ |
150 | |
151 | private: | |
152 | DECLARE_EVENT_TABLE() | |
153 | }; | |
154 | ||
155 | // A trivial exception class | |
156 | class MyException | |
157 | { | |
158 | public: | |
159 | MyException(const wxString& msg) : m_msg(msg) { } | |
160 | ||
161 | const wxChar *what() const { return m_msg.c_str(); } | |
162 | ||
163 | private: | |
164 | wxString m_msg; | |
165 | }; | |
166 | ||
cc9a3957 VZ |
167 | // Another exception class which just has to be different from anything else |
168 | class UnhandledException | |
169 | { | |
170 | }; | |
171 | ||
556b8c1a VZ |
172 | // ---------------------------------------------------------------------------- |
173 | // constants | |
174 | // ---------------------------------------------------------------------------- | |
175 | ||
176 | // IDs for the controls and the menu commands | |
177 | enum | |
178 | { | |
cc9a3957 | 179 | // control ids and menu items |
b6b375a9 | 180 | Except_ThrowInt = wxID_HIGHEST, |
cc9a3957 | 181 | Except_ThrowString, |
556b8c1a | 182 | Except_ThrowObject, |
cc9a3957 | 183 | Except_ThrowUnhandled, |
f78e4942 | 184 | Except_Crash, |
b6b375a9 | 185 | #if wxUSE_ON_FATAL_EXCEPTION |
cc9a3957 | 186 | Except_HandleCrash, |
3eeb7e10 VZ |
187 | #endif // wxUSE_ON_FATAL_EXCEPTION |
188 | #ifdef __WXDEBUG__ | |
189 | Except_ShowAssert, | |
190 | #endif // __WXDEBUG__ | |
556b8c1a VZ |
191 | Except_Dialog, |
192 | ||
193 | Except_Quit = wxID_EXIT, | |
194 | Except_About = wxID_ABOUT | |
195 | }; | |
196 | ||
197 | // ---------------------------------------------------------------------------- | |
be5a51fb | 198 | // event tables and other macros for wxWidgets |
556b8c1a VZ |
199 | // ---------------------------------------------------------------------------- |
200 | ||
be5a51fb | 201 | // the event tables connect the wxWidgets events with the functions (event |
556b8c1a VZ |
202 | // handlers) which process them. It can be also done at run-time, but for the |
203 | // simple menu events like this the static method is much simpler. | |
204 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
205 | EVT_MENU(Except_Quit, MyFrame::OnQuit) | |
206 | EVT_MENU(Except_About, MyFrame::OnAbout) | |
207 | EVT_MENU(Except_Dialog, MyFrame::OnDialog) | |
cc9a3957 | 208 | EVT_MENU(Except_ThrowInt, MyFrame::OnThrowInt) |
556b8c1a | 209 | EVT_MENU(Except_ThrowString, MyFrame::OnThrowString) |
cc9a3957 VZ |
210 | EVT_MENU(Except_ThrowObject, MyFrame::OnThrowObject) |
211 | EVT_MENU(Except_ThrowUnhandled, MyFrame::OnThrowUnhandled) | |
212 | EVT_MENU(Except_Crash, MyFrame::OnCrash) | |
b6b375a9 | 213 | #if wxUSE_ON_FATAL_EXCEPTION |
cc9a3957 | 214 | EVT_MENU(Except_HandleCrash, MyFrame::OnHandleCrash) |
3eeb7e10 VZ |
215 | #endif // wxUSE_ON_FATAL_EXCEPTION |
216 | #ifdef __WXDEBUG__ | |
217 | EVT_MENU(Except_ShowAssert, MyFrame::OnShowAssert) | |
218 | #endif // __WXDEBUG__ | |
556b8c1a VZ |
219 | END_EVENT_TABLE() |
220 | ||
221 | BEGIN_EVENT_TABLE(MyDialog, wxDialog) | |
222 | EVT_BUTTON(Except_ThrowInt, MyDialog::OnThrowInt) | |
223 | EVT_BUTTON(Except_ThrowObject, MyDialog::OnThrowObject) | |
f78e4942 | 224 | EVT_BUTTON(Except_Crash, MyDialog::OnCrash) |
556b8c1a VZ |
225 | END_EVENT_TABLE() |
226 | ||
be5a51fb | 227 | // Create a new application object: this macro will allow wxWidgets to create |
556b8c1a VZ |
228 | // the application object during program execution (it's better than using a |
229 | // static object for many reasons) and also implements the accessor function | |
230 | // wxGetApp() which will return the reference of the right type (i.e. MyApp and | |
231 | // not wxApp) | |
232 | IMPLEMENT_APP(MyApp) | |
233 | ||
234 | // ============================================================================ | |
235 | // MyApp implementation | |
236 | // ============================================================================ | |
237 | ||
238 | // 'Main program' equivalent: the program execution "starts" here | |
239 | bool MyApp::OnInit() | |
240 | { | |
45e6e6f8 VZ |
241 | if ( !wxApp::OnInit() ) |
242 | return false; | |
243 | ||
556b8c1a | 244 | // create the main application window |
cc9a3957 | 245 | MyFrame *frame = new MyFrame(); |
556b8c1a VZ |
246 | |
247 | // and show it (the frames, unlike simple controls, are not shown when | |
248 | // created initially) | |
249 | frame->Show(true); | |
250 | ||
251 | // success: wxApp::OnRun() will be called which will enter the main message | |
252 | // loop and the application will run. If we returned false here, the | |
253 | // application would exit immediately. | |
254 | return true; | |
255 | } | |
256 | ||
cc9a3957 | 257 | bool MyApp::OnExceptionInMainLoop() |
556b8c1a VZ |
258 | { |
259 | try | |
260 | { | |
cc9a3957 | 261 | throw; |
556b8c1a VZ |
262 | } |
263 | catch ( int i ) | |
264 | { | |
cc9a3957 | 265 | wxLogWarning(_T("Caught an int %d in MyApp."), i); |
556b8c1a | 266 | } |
cc9a3957 VZ |
267 | catch ( MyException& e ) |
268 | { | |
269 | wxLogWarning(_T("Caught MyException(%s) in MyApp."), e.what()); | |
270 | } | |
271 | catch ( ... ) | |
272 | { | |
273 | throw; | |
274 | } | |
275 | ||
276 | return true; | |
556b8c1a VZ |
277 | } |
278 | ||
279 | void MyApp::OnUnhandledException() | |
280 | { | |
cc9a3957 VZ |
281 | // this shows how we may let some exception propagate uncaught |
282 | try | |
283 | { | |
284 | throw; | |
285 | } | |
286 | catch ( UnhandledException& ) | |
287 | { | |
288 | throw; | |
289 | } | |
290 | catch ( ... ) | |
291 | { | |
292 | wxMessageBox(_T("Unhandled exception caught, program will terminate."), | |
293 | _T("wxExcept Sample"), wxOK | wxICON_ERROR); | |
294 | } | |
295 | } | |
296 | ||
297 | void MyApp::OnFatalException() | |
298 | { | |
299 | wxMessageBox(_T("Program has crashed and will terminate."), | |
556b8c1a VZ |
300 | _T("wxExcept Sample"), wxOK | wxICON_ERROR); |
301 | } | |
302 | ||
3eeb7e10 VZ |
303 | #ifdef __WXDEBUG__ |
304 | ||
305 | void MyApp::OnAssert(const wxChar *file, | |
306 | int line, | |
307 | const wxChar *cond, | |
308 | const wxChar *msg) | |
309 | { | |
310 | // we don't have anything special to do here | |
311 | wxApp::OnAssert(file, line, cond, msg); | |
312 | } | |
313 | ||
314 | #endif // __WXDEBUG__ | |
315 | ||
556b8c1a VZ |
316 | // ============================================================================ |
317 | // MyFrame implementation | |
318 | // ============================================================================ | |
319 | ||
320 | // frame constructor | |
cc9a3957 VZ |
321 | MyFrame::MyFrame() |
322 | : wxFrame(NULL, wxID_ANY, _T("Except wxWidgets App"), | |
323 | wxPoint(50, 50), wxSize(450, 340)) | |
556b8c1a VZ |
324 | { |
325 | // set the frame icon | |
d32b901b | 326 | SetIcon(wxICON(sample)); |
556b8c1a VZ |
327 | |
328 | #if wxUSE_MENUS | |
329 | // create a menu bar | |
330 | wxMenu *menuFile = new wxMenu; | |
331 | menuFile->Append(Except_Dialog, _T("Show &dialog\tCtrl-D")); | |
cc9a3957 VZ |
332 | menuFile->AppendSeparator(); |
333 | menuFile->Append(Except_ThrowInt, _T("Throw an &int\tCtrl-I")); | |
556b8c1a | 334 | menuFile->Append(Except_ThrowString, _T("Throw a &string\tCtrl-S")); |
cc9a3957 VZ |
335 | menuFile->Append(Except_ThrowObject, _T("Throw an &object\tCtrl-O")); |
336 | menuFile->Append(Except_ThrowUnhandled, | |
337 | _T("Throw &unhandled exception\tCtrl-U")); | |
338 | menuFile->Append(Except_Crash, _T("&Crash\tCtrl-C")); | |
339 | menuFile->AppendSeparator(); | |
b6b375a9 | 340 | #if wxUSE_ON_FATAL_EXCEPTION |
cc9a3957 | 341 | menuFile->AppendCheckItem(Except_HandleCrash, _T("&Handle crashes\tCtrl-H")); |
556b8c1a | 342 | menuFile->AppendSeparator(); |
3eeb7e10 VZ |
343 | #endif // wxUSE_ON_FATAL_EXCEPTION |
344 | #ifdef __WXDEBUG__ | |
345 | menuFile->Append(Except_ShowAssert, _T("Provoke &assert failure\tCtrl-A")); | |
346 | menuFile->AppendSeparator(); | |
347 | #endif // __WXDEBUG__ | |
556b8c1a VZ |
348 | menuFile->Append(Except_Quit, _T("E&xit\tCtrl-Q"), _T("Quit this program")); |
349 | ||
350 | wxMenu *helpMenu = new wxMenu; | |
351 | helpMenu->Append(Except_About, _T("&About...\tF1"), _T("Show about dialog")); | |
352 | ||
353 | // now append the freshly created menu to the menu bar... | |
354 | wxMenuBar *menuBar = new wxMenuBar(); | |
355 | menuBar->Append(menuFile, _T("&File")); | |
356 | menuBar->Append(helpMenu, _T("&Help")); | |
357 | ||
358 | // ... and attach this menu bar to the frame | |
359 | SetMenuBar(menuBar); | |
360 | #endif // wxUSE_MENUS | |
361 | ||
362 | #if wxUSE_STATUSBAR && !defined(__WXWINCE__) | |
363 | // create a status bar just for fun (by default with 1 pane only) | |
364 | CreateStatusBar(2); | |
be5a51fb | 365 | SetStatusText(_T("Welcome to wxWidgets!")); |
556b8c1a VZ |
366 | #endif // wxUSE_STATUSBAR |
367 | } | |
368 | ||
369 | bool MyFrame::ProcessEvent(wxEvent& event) | |
370 | { | |
371 | try | |
372 | { | |
373 | return wxFrame::ProcessEvent(event); | |
374 | } | |
375 | catch ( const wxChar *msg ) | |
376 | { | |
377 | wxLogMessage(_T("Caught a string \"%s\" in MyFrame"), msg); | |
378 | ||
379 | return true; | |
380 | } | |
381 | } | |
382 | ||
383 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) | |
384 | { | |
385 | // true is to force the frame to close | |
386 | Close(true); | |
387 | } | |
388 | ||
389 | void MyFrame::OnDialog(wxCommandEvent& WXUNUSED(event)) | |
390 | { | |
df5a063f VZ |
391 | try |
392 | { | |
393 | MyDialog dlg(this); | |
556b8c1a | 394 | |
df5a063f VZ |
395 | dlg.ShowModal(); |
396 | } | |
397 | catch ( ... ) | |
398 | { | |
cc9a3957 VZ |
399 | wxLogWarning(_T("An exception in MyDialog")); |
400 | ||
df5a063f VZ |
401 | Destroy(); |
402 | throw; | |
403 | } | |
556b8c1a VZ |
404 | } |
405 | ||
cc9a3957 VZ |
406 | void MyFrame::OnThrowInt(wxCommandEvent& WXUNUSED(event)) |
407 | { | |
408 | throw -17; | |
409 | } | |
410 | ||
556b8c1a VZ |
411 | void MyFrame::OnThrowString(wxCommandEvent& WXUNUSED(event)) |
412 | { | |
cc9a3957 VZ |
413 | throw _T("string thrown from MyFrame"); |
414 | } | |
415 | ||
416 | void MyFrame::OnThrowObject(wxCommandEvent& WXUNUSED(event)) | |
417 | { | |
418 | throw MyException(_T("Exception thrown from MyFrame")); | |
419 | } | |
420 | ||
421 | void MyFrame::OnThrowUnhandled(wxCommandEvent& WXUNUSED(event)) | |
422 | { | |
423 | throw UnhandledException(); | |
424 | } | |
425 | ||
426 | void MyFrame::OnCrash(wxCommandEvent& WXUNUSED(event)) | |
427 | { | |
428 | DoCrash(); | |
429 | } | |
430 | ||
b6b375a9 WS |
431 | #if wxUSE_ON_FATAL_EXCEPTION |
432 | ||
cc9a3957 VZ |
433 | void MyFrame::OnHandleCrash(wxCommandEvent& event) |
434 | { | |
435 | wxHandleFatalExceptions(event.IsChecked()); | |
556b8c1a VZ |
436 | } |
437 | ||
3eeb7e10 VZ |
438 | #endif // wxUSE_ON_FATAL_EXCEPTION |
439 | ||
440 | #ifdef __WXDEBUG__ | |
441 | ||
442 | void MyFrame::OnShowAssert(wxCommandEvent& WXUNUSED(event)) | |
443 | { | |
444 | // provoke an assert from wxArrayString | |
445 | wxArrayString arr; | |
446 | arr[0]; | |
447 | } | |
448 | ||
449 | #endif // __WXDEBUG__ | |
b6b375a9 | 450 | |
556b8c1a VZ |
451 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) |
452 | { | |
453 | wxString msg; | |
454 | msg.Printf( _T("This is the About dialog of the except sample.\n") | |
455 | _T("Welcome to %s"), wxVERSION_STRING); | |
456 | ||
457 | wxMessageBox(msg, _T("About Except"), wxOK | wxICON_INFORMATION, this); | |
458 | } | |
459 | ||
460 | // ============================================================================ | |
461 | // MyDialog implementation | |
462 | // ============================================================================ | |
463 | ||
464 | MyDialog::MyDialog(wxFrame *parent) | |
30deac1f | 465 | : wxDialog(parent, wxID_ANY, wxString(_T("Throw exception dialog"))) |
556b8c1a VZ |
466 | { |
467 | wxSizer *sizerTop = new wxBoxSizer(wxVERTICAL); | |
468 | ||
469 | sizerTop->Add(new wxButton(this, Except_ThrowInt, _T("Throw &int")), | |
470 | 0, wxCENTRE | wxALL, 5); | |
471 | sizerTop->Add(new wxButton(this, Except_ThrowObject, _T("Throw &object")), | |
472 | 0, wxCENTRE | wxALL, 5); | |
f78e4942 VZ |
473 | sizerTop->Add(new wxButton(this, Except_Crash, _T("&Crash")), |
474 | 0, wxCENTRE | wxALL, 5); | |
556b8c1a VZ |
475 | sizerTop->Add(new wxButton(this, wxID_CANCEL, _T("&Cancel")), |
476 | 0, wxCENTRE | wxALL, 5); | |
477 | ||
478 | SetSizer(sizerTop); | |
479 | sizerTop->Fit(this); | |
480 | } | |
481 | ||
482 | void MyDialog::OnThrowInt(wxCommandEvent& WXUNUSED(event)) | |
483 | { | |
484 | throw 17; | |
485 | } | |
486 | ||
487 | void MyDialog::OnThrowObject(wxCommandEvent& WXUNUSED(event)) | |
488 | { | |
cc9a3957 | 489 | throw MyException(_T("Exception thrown from MyDialog")); |
556b8c1a VZ |
490 | } |
491 | ||
f78e4942 VZ |
492 | void MyDialog::OnCrash(wxCommandEvent& WXUNUSED(event)) |
493 | { | |
cc9a3957 | 494 | DoCrash(); |
f78e4942 VZ |
495 | } |
496 |