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