]> git.saurik.com Git - wxWidgets.git/blame - samples/except/except.cpp
better variant support, fixes #11114
[wxWidgets.git] / samples / except / except.cpp
CommitLineData
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
62static 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
73class MyApp : public wxApp
74{
75public:
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
105class MyFrame : public wxFrame
106{
107public:
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 125
004867db
FM
126protected:
127
556b8c1a
VZ
128 // 1st-level exception handling: we overload ProcessEvent() to be able to
129 // catch exceptions which occur in MyFrame methods here
130 virtual bool ProcessEvent(wxEvent& event);
131
3eeb7e10
VZ
132#ifdef __WXDEBUG__
133 // show how an assert failure message box looks like
134 void OnShowAssert(wxCommandEvent& event);
135#endif // __WXDEBUG__
136
556b8c1a 137private:
be5a51fb 138 // any class wishing to process wxWidgets events must use this macro
556b8c1a
VZ
139 DECLARE_EVENT_TABLE()
140};
141
142// A simple dialog which has only some buttons to throw exceptions
143class MyDialog : public wxDialog
144{
145public:
146 MyDialog(wxFrame *parent);
147
148 // event handlers
149 void OnThrowInt(wxCommandEvent& event);
150 void OnThrowObject(wxCommandEvent& event);
f78e4942 151 void OnCrash(wxCommandEvent& event);
556b8c1a
VZ
152
153private:
154 DECLARE_EVENT_TABLE()
155};
156
157// A trivial exception class
158class MyException
159{
160public:
161 MyException(const wxString& msg) : m_msg(msg) { }
162
163 const wxChar *what() const { return m_msg.c_str(); }
164
165private:
166 wxString m_msg;
167};
168
cc9a3957
VZ
169// Another exception class which just has to be different from anything else
170class UnhandledException
171{
172};
173
556b8c1a
VZ
174// ----------------------------------------------------------------------------
175// constants
176// ----------------------------------------------------------------------------
177
178// IDs for the controls and the menu commands
179enum
180{
cc9a3957 181 // control ids and menu items
b6b375a9 182 Except_ThrowInt = wxID_HIGHEST,
cc9a3957 183 Except_ThrowString,
556b8c1a 184 Except_ThrowObject,
cc9a3957 185 Except_ThrowUnhandled,
f78e4942 186 Except_Crash,
b6b375a9 187#if wxUSE_ON_FATAL_EXCEPTION
cc9a3957 188 Except_HandleCrash,
3eeb7e10
VZ
189#endif // wxUSE_ON_FATAL_EXCEPTION
190#ifdef __WXDEBUG__
191 Except_ShowAssert,
192#endif // __WXDEBUG__
556b8c1a
VZ
193 Except_Dialog,
194
195 Except_Quit = wxID_EXIT,
196 Except_About = wxID_ABOUT
197};
198
199// ----------------------------------------------------------------------------
be5a51fb 200// event tables and other macros for wxWidgets
556b8c1a
VZ
201// ----------------------------------------------------------------------------
202
be5a51fb 203// the event tables connect the wxWidgets events with the functions (event
556b8c1a
VZ
204// handlers) which process them. It can be also done at run-time, but for the
205// simple menu events like this the static method is much simpler.
206BEGIN_EVENT_TABLE(MyFrame, wxFrame)
207 EVT_MENU(Except_Quit, MyFrame::OnQuit)
208 EVT_MENU(Except_About, MyFrame::OnAbout)
209 EVT_MENU(Except_Dialog, MyFrame::OnDialog)
cc9a3957 210 EVT_MENU(Except_ThrowInt, MyFrame::OnThrowInt)
556b8c1a 211 EVT_MENU(Except_ThrowString, MyFrame::OnThrowString)
cc9a3957
VZ
212 EVT_MENU(Except_ThrowObject, MyFrame::OnThrowObject)
213 EVT_MENU(Except_ThrowUnhandled, MyFrame::OnThrowUnhandled)
214 EVT_MENU(Except_Crash, MyFrame::OnCrash)
b6b375a9 215#if wxUSE_ON_FATAL_EXCEPTION
cc9a3957 216 EVT_MENU(Except_HandleCrash, MyFrame::OnHandleCrash)
3eeb7e10
VZ
217#endif // wxUSE_ON_FATAL_EXCEPTION
218#ifdef __WXDEBUG__
219 EVT_MENU(Except_ShowAssert, MyFrame::OnShowAssert)
220#endif // __WXDEBUG__
556b8c1a
VZ
221END_EVENT_TABLE()
222
223BEGIN_EVENT_TABLE(MyDialog, wxDialog)
224 EVT_BUTTON(Except_ThrowInt, MyDialog::OnThrowInt)
225 EVT_BUTTON(Except_ThrowObject, MyDialog::OnThrowObject)
f78e4942 226 EVT_BUTTON(Except_Crash, MyDialog::OnCrash)
556b8c1a
VZ
227END_EVENT_TABLE()
228
be5a51fb 229// Create a new application object: this macro will allow wxWidgets to create
556b8c1a
VZ
230// the application object during program execution (it's better than using a
231// static object for many reasons) and also implements the accessor function
232// wxGetApp() which will return the reference of the right type (i.e. MyApp and
233// not wxApp)
234IMPLEMENT_APP(MyApp)
235
236// ============================================================================
237// MyApp implementation
238// ============================================================================
239
240// 'Main program' equivalent: the program execution "starts" here
241bool MyApp::OnInit()
242{
45e6e6f8
VZ
243 if ( !wxApp::OnInit() )
244 return false;
245
556b8c1a 246 // create the main application window
cc9a3957 247 MyFrame *frame = new MyFrame();
556b8c1a
VZ
248
249 // and show it (the frames, unlike simple controls, are not shown when
250 // created initially)
251 frame->Show(true);
252
253 // success: wxApp::OnRun() will be called which will enter the main message
254 // loop and the application will run. If we returned false here, the
255 // application would exit immediately.
256 return true;
257}
258
cc9a3957 259bool MyApp::OnExceptionInMainLoop()
556b8c1a
VZ
260{
261 try
262 {
cc9a3957 263 throw;
556b8c1a
VZ
264 }
265 catch ( int i )
266 {
9a83f860 267 wxLogWarning(wxT("Caught an int %d in MyApp."), i);
556b8c1a 268 }
cc9a3957
VZ
269 catch ( MyException& e )
270 {
9a83f860 271 wxLogWarning(wxT("Caught MyException(%s) in MyApp."), e.what());
cc9a3957
VZ
272 }
273 catch ( ... )
274 {
275 throw;
276 }
277
278 return true;
556b8c1a
VZ
279}
280
281void MyApp::OnUnhandledException()
282{
cc9a3957
VZ
283 // this shows how we may let some exception propagate uncaught
284 try
285 {
286 throw;
287 }
288 catch ( UnhandledException& )
289 {
290 throw;
291 }
292 catch ( ... )
293 {
9a83f860
VZ
294 wxMessageBox(wxT("Unhandled exception caught, program will terminate."),
295 wxT("wxExcept Sample"), wxOK | wxICON_ERROR);
cc9a3957
VZ
296 }
297}
298
299void MyApp::OnFatalException()
300{
9a83f860
VZ
301 wxMessageBox(wxT("Program has crashed and will terminate."),
302 wxT("wxExcept Sample"), wxOK | wxICON_ERROR);
556b8c1a
VZ
303}
304
3eeb7e10
VZ
305#ifdef __WXDEBUG__
306
307void MyApp::OnAssert(const wxChar *file,
308 int line,
309 const wxChar *cond,
310 const wxChar *msg)
311{
312 // we don't have anything special to do here
313 wxApp::OnAssert(file, line, cond, msg);
314}
315
316#endif // __WXDEBUG__
317
556b8c1a
VZ
318// ============================================================================
319// MyFrame implementation
320// ============================================================================
321
322// frame constructor
cc9a3957 323MyFrame::MyFrame()
9a83f860 324 : wxFrame(NULL, wxID_ANY, wxT("Except wxWidgets App"),
cc9a3957 325 wxPoint(50, 50), wxSize(450, 340))
556b8c1a
VZ
326{
327 // set the frame icon
d32b901b 328 SetIcon(wxICON(sample));
556b8c1a
VZ
329
330#if wxUSE_MENUS
331 // create a menu bar
332 wxMenu *menuFile = new wxMenu;
9a83f860 333 menuFile->Append(Except_Dialog, wxT("Show &dialog\tCtrl-D"));
cc9a3957 334 menuFile->AppendSeparator();
9a83f860
VZ
335 menuFile->Append(Except_ThrowInt, wxT("Throw an &int\tCtrl-I"));
336 menuFile->Append(Except_ThrowString, wxT("Throw a &string\tCtrl-S"));
337 menuFile->Append(Except_ThrowObject, wxT("Throw an &object\tCtrl-O"));
cc9a3957 338 menuFile->Append(Except_ThrowUnhandled,
9a83f860
VZ
339 wxT("Throw &unhandled exception\tCtrl-U"));
340 menuFile->Append(Except_Crash, wxT("&Crash\tCtrl-C"));
cc9a3957 341 menuFile->AppendSeparator();
b6b375a9 342#if wxUSE_ON_FATAL_EXCEPTION
9a83f860 343 menuFile->AppendCheckItem(Except_HandleCrash, wxT("&Handle crashes\tCtrl-H"));
556b8c1a 344 menuFile->AppendSeparator();
3eeb7e10
VZ
345#endif // wxUSE_ON_FATAL_EXCEPTION
346#ifdef __WXDEBUG__
9a83f860 347 menuFile->Append(Except_ShowAssert, wxT("Provoke &assert failure\tCtrl-A"));
3eeb7e10
VZ
348 menuFile->AppendSeparator();
349#endif // __WXDEBUG__
9a83f860 350 menuFile->Append(Except_Quit, wxT("E&xit\tCtrl-Q"), wxT("Quit this program"));
556b8c1a
VZ
351
352 wxMenu *helpMenu = new wxMenu;
9a83f860 353 helpMenu->Append(Except_About, wxT("&About...\tF1"), wxT("Show about dialog"));
556b8c1a
VZ
354
355 // now append the freshly created menu to the menu bar...
356 wxMenuBar *menuBar = new wxMenuBar();
9a83f860
VZ
357 menuBar->Append(menuFile, wxT("&File"));
358 menuBar->Append(helpMenu, wxT("&Help"));
556b8c1a
VZ
359
360 // ... and attach this menu bar to the frame
361 SetMenuBar(menuBar);
362#endif // wxUSE_MENUS
363
364#if wxUSE_STATUSBAR && !defined(__WXWINCE__)
365 // create a status bar just for fun (by default with 1 pane only)
366 CreateStatusBar(2);
9a83f860 367 SetStatusText(wxT("Welcome to wxWidgets!"));
556b8c1a
VZ
368#endif // wxUSE_STATUSBAR
369}
370
371bool MyFrame::ProcessEvent(wxEvent& event)
372{
373 try
374 {
375 return wxFrame::ProcessEvent(event);
376 }
377 catch ( const wxChar *msg )
378 {
9a83f860 379 wxLogMessage(wxT("Caught a string \"%s\" in MyFrame"), msg);
556b8c1a
VZ
380
381 return true;
382 }
383}
384
385void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
386{
387 // true is to force the frame to close
388 Close(true);
389}
390
391void MyFrame::OnDialog(wxCommandEvent& WXUNUSED(event))
392{
df5a063f
VZ
393 try
394 {
395 MyDialog dlg(this);
556b8c1a 396
df5a063f
VZ
397 dlg.ShowModal();
398 }
399 catch ( ... )
400 {
9a83f860 401 wxLogWarning(wxT("An exception in MyDialog"));
cc9a3957 402
df5a063f
VZ
403 Destroy();
404 throw;
405 }
556b8c1a
VZ
406}
407
cc9a3957
VZ
408void MyFrame::OnThrowInt(wxCommandEvent& WXUNUSED(event))
409{
410 throw -17;
411}
412
556b8c1a
VZ
413void MyFrame::OnThrowString(wxCommandEvent& WXUNUSED(event))
414{
9a83f860 415 throw wxT("string thrown from MyFrame");
cc9a3957
VZ
416}
417
418void MyFrame::OnThrowObject(wxCommandEvent& WXUNUSED(event))
419{
9a83f860 420 throw MyException(wxT("Exception thrown from MyFrame"));
cc9a3957
VZ
421}
422
423void MyFrame::OnThrowUnhandled(wxCommandEvent& WXUNUSED(event))
424{
425 throw UnhandledException();
426}
427
428void MyFrame::OnCrash(wxCommandEvent& WXUNUSED(event))
429{
430 DoCrash();
431}
432
b6b375a9
WS
433#if wxUSE_ON_FATAL_EXCEPTION
434
cc9a3957
VZ
435void MyFrame::OnHandleCrash(wxCommandEvent& event)
436{
437 wxHandleFatalExceptions(event.IsChecked());
556b8c1a
VZ
438}
439
3eeb7e10
VZ
440#endif // wxUSE_ON_FATAL_EXCEPTION
441
442#ifdef __WXDEBUG__
443
444void MyFrame::OnShowAssert(wxCommandEvent& WXUNUSED(event))
445{
446 // provoke an assert from wxArrayString
447 wxArrayString arr;
448 arr[0];
449}
450
451#endif // __WXDEBUG__
b6b375a9 452
556b8c1a
VZ
453void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
454{
455 wxString msg;
9a83f860
VZ
456 msg.Printf( wxT("This is the About dialog of the except sample.\n")
457 wxT("Welcome to %s"), wxVERSION_STRING);
556b8c1a 458
9a83f860 459 wxMessageBox(msg, wxT("About Except"), wxOK | wxICON_INFORMATION, this);
556b8c1a
VZ
460}
461
462// ============================================================================
463// MyDialog implementation
464// ============================================================================
465
466MyDialog::MyDialog(wxFrame *parent)
9a83f860 467 : wxDialog(parent, wxID_ANY, wxString(wxT("Throw exception dialog")))
556b8c1a
VZ
468{
469 wxSizer *sizerTop = new wxBoxSizer(wxVERTICAL);
470
9a83f860 471 sizerTop->Add(new wxButton(this, Except_ThrowInt, wxT("Throw &int")),
556b8c1a 472 0, wxCENTRE | wxALL, 5);
9a83f860 473 sizerTop->Add(new wxButton(this, Except_ThrowObject, wxT("Throw &object")),
556b8c1a 474 0, wxCENTRE | wxALL, 5);
9a83f860 475 sizerTop->Add(new wxButton(this, Except_Crash, wxT("&Crash")),
f78e4942 476 0, wxCENTRE | wxALL, 5);
9a83f860 477 sizerTop->Add(new wxButton(this, wxID_CANCEL, wxT("&Cancel")),
556b8c1a
VZ
478 0, wxCENTRE | wxALL, 5);
479
92c01615 480 SetSizerAndFit(sizerTop);
556b8c1a
VZ
481}
482
483void MyDialog::OnThrowInt(wxCommandEvent& WXUNUSED(event))
484{
485 throw 17;
486}
487
488void MyDialog::OnThrowObject(wxCommandEvent& WXUNUSED(event))
489{
9a83f860 490 throw MyException(wxT("Exception thrown from MyDialog"));
556b8c1a
VZ
491}
492
f78e4942
VZ
493void MyDialog::OnCrash(wxCommandEvent& WXUNUSED(event))
494{
cc9a3957 495 DoCrash();
f78e4942
VZ
496}
497