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