1 /////////////////////////////////////////////////////////////////////////////
2 // Name: samples/except/except.cpp
3 // Purpose: shows how C++ exceptions can be used in wxWidgets
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2003-2005 Vadim Zeitlin
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
27 #error "This sample only works with wxUSE_EXCEPTIONS == 1"
28 #endif // !wxUSE_EXCEPTIONS
30 // for all others, include the necessary headers (this file is usually all you
31 // need because it includes almost all "standard" wxWidgets headers)
37 #include "wx/dialog.h"
40 #include "wx/button.h"
44 #include "wx/msgdlg.h"
47 #include "wx/thread.h"
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 // the application icon (under Windows and OS/2 it is in resources)
55 #ifndef wxHAS_IMAGES_IN_RESOURCES
56 #include "../sample.xpm"
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
66 strcpy(p
, "Let's crash");
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
73 // Define a new application type, each program should derive a class from wxApp
74 class MyApp
: public wxApp
77 // override base class virtuals
78 // ----------------------------
81 virtual bool OnInit();
83 // 2nd-level exception handling: we get all the exceptions occurring in any
85 virtual bool OnExceptionInMainLoop();
87 // 3rd, and final, level exception handling: whenever an unhandled
88 // exception is caught, this function is called
89 virtual void OnUnhandledException();
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();
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
,
104 // Define a new frame type: this is going to be our main frame
105 class MyFrame
: public wxFrame
111 // event handlers (these functions should _not_ be virtual)
112 void OnQuit(wxCommandEvent
& event
);
113 void OnAbout(wxCommandEvent
& event
);
114 void OnDialog(wxCommandEvent
& event
);
116 void OnThrowInt(wxCommandEvent
& event
);
117 void OnThrowString(wxCommandEvent
& event
);
118 void OnThrowObject(wxCommandEvent
& event
);
119 void OnThrowUnhandled(wxCommandEvent
& event
);
121 void OnCrash(wxCommandEvent
& event
);
122 void OnTrap(wxCommandEvent
& event
);
123 #if wxUSE_ON_FATAL_EXCEPTION
124 void OnHandleCrash(wxCommandEvent
& event
);
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
);
133 // provoke assert in main or worker thread
135 // this is used to show how an assert failure message box looks like
136 void OnShowAssert(wxCommandEvent
& event
);
138 void OnShowAssertInThread(wxCommandEvent
& event
);
139 #endif // wxUSE_THREADS
142 // any class wishing to process wxWidgets events must use this macro
143 DECLARE_EVENT_TABLE()
146 // A simple dialog which has only some buttons to throw exceptions
147 class MyDialog
: public wxDialog
150 MyDialog(wxFrame
*parent
);
153 void OnThrowInt(wxCommandEvent
& event
);
154 void OnThrowObject(wxCommandEvent
& event
);
155 void OnCrash(wxCommandEvent
& event
);
158 DECLARE_EVENT_TABLE()
161 // A trivial exception class
165 MyException(const wxString
& msg
) : m_msg(msg
) { }
167 const wxChar
*what() const { return m_msg
.c_str(); }
173 // Another exception class which just has to be different from anything else
174 class UnhandledException
178 // ----------------------------------------------------------------------------
180 // ----------------------------------------------------------------------------
182 // IDs for the controls and the menu commands
185 // control ids and menu items
186 Except_ThrowInt
= wxID_HIGHEST
,
189 Except_ThrowUnhandled
,
192 #if wxUSE_ON_FATAL_EXCEPTION
194 #endif // wxUSE_ON_FATAL_EXCEPTION
197 Except_ShowAssertInThread
,
198 #endif // wxUSE_THREADS
201 Except_Quit
= wxID_EXIT
,
202 Except_About
= wxID_ABOUT
205 // ----------------------------------------------------------------------------
206 // event tables and other macros for wxWidgets
207 // ----------------------------------------------------------------------------
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.
212 BEGIN_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
)
227 EVT_MENU(Except_ShowAssertInThread
, MyFrame::OnShowAssertInThread
)
228 #endif // wxUSE_THREADS
231 BEGIN_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
)
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
244 // ============================================================================
245 // MyApp implementation
246 // ============================================================================
248 // 'Main program' equivalent: the program execution "starts" here
251 if ( !wxApp::OnInit() )
254 // create the main application window
255 MyFrame
*frame
= new MyFrame();
257 // and show it (the frames, unlike simple controls, are not shown when
258 // created initially)
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.
267 bool MyApp::OnExceptionInMainLoop()
275 wxLogWarning(wxT("Caught an int %d in MyApp."), i
);
277 catch ( MyException
& e
)
279 wxLogWarning(wxT("Caught MyException(%s) in MyApp."), e
.what());
289 void MyApp::OnUnhandledException()
291 // this shows how we may let some exception propagate uncaught
296 catch ( UnhandledException
& )
302 wxMessageBox(wxT("Unhandled exception caught, program will terminate."),
303 wxT("wxExcept Sample"), wxOK
| wxICON_ERROR
);
307 void MyApp::OnFatalException()
309 wxMessageBox(wxT("Program has crashed and will terminate."),
310 wxT("wxExcept Sample"), wxOK
| wxICON_ERROR
);
313 void MyApp::OnAssertFailure(const wxChar
*file
,
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() ||
324 wxString::Format("An assert failed in %s().", func
) +
326 "Do you want to call the default assert handler?",
328 wxYES_NO
| wxICON_QUESTION
331 wxApp::OnAssertFailure(file
, line
, func
, cond
, msg
);
335 // ============================================================================
336 // MyFrame implementation
337 // ============================================================================
341 : wxFrame(NULL
, wxID_ANY
, wxT("Except wxWidgets App"),
342 wxPoint(50, 50), wxSize(450, 340))
344 // set the frame icon
345 SetIcon(wxICON(sample
));
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"));
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"));
373 wxMenu
*helpMenu
= new wxMenu
;
374 helpMenu
->Append(Except_About
, wxT("&About\tF1"), wxT("Show about dialog"));
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"));
381 // ... and attach this menu bar to the frame
383 #endif // wxUSE_MENUS
385 #if wxUSE_STATUSBAR && !defined(__WXWINCE__)
386 // create a status bar just for fun (by default with 1 pane only)
388 SetStatusText(wxT("Welcome to wxWidgets!"));
389 #endif // wxUSE_STATUSBAR
392 bool MyFrame::ProcessEvent(wxEvent
& event
)
396 return wxFrame::ProcessEvent(event
);
398 catch ( const wxChar
*msg
)
400 wxLogMessage(wxT("Caught a string \"%s\" in MyFrame"), msg
);
406 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
408 // true is to force the frame to close
412 void MyFrame::OnDialog(wxCommandEvent
& WXUNUSED(event
))
422 wxLogWarning(wxT("An exception in MyDialog"));
429 void MyFrame::OnThrowInt(wxCommandEvent
& WXUNUSED(event
))
434 void MyFrame::OnThrowString(wxCommandEvent
& WXUNUSED(event
))
436 throw wxT("string thrown from MyFrame");
439 void MyFrame::OnThrowObject(wxCommandEvent
& WXUNUSED(event
))
441 throw MyException(wxT("Exception thrown from MyFrame"));
444 void MyFrame::OnThrowUnhandled(wxCommandEvent
& WXUNUSED(event
))
446 throw UnhandledException();
449 void MyFrame::OnCrash(wxCommandEvent
& WXUNUSED(event
))
454 void MyFrame::OnTrap(wxCommandEvent
& WXUNUSED(event
))
459 #if wxUSE_ON_FATAL_EXCEPTION
461 void MyFrame::OnHandleCrash(wxCommandEvent
& event
)
463 wxHandleFatalExceptions(event
.IsChecked());
466 #endif // wxUSE_ON_FATAL_EXCEPTION
468 void MyFrame::OnShowAssert(wxCommandEvent
& WXUNUSED(event
))
470 // provoke an assert from wxArrayString
477 void MyFrame::OnShowAssertInThread(wxCommandEvent
& WXUNUSED(event
))
479 class AssertThread
: public wxThread
483 : wxThread(wxTHREAD_JOINABLE
)
488 virtual void *Entry()
490 wxFAIL_MSG("Test assert in another thread.");
502 #endif // wxUSE_THREADS
504 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
507 msg
.Printf( wxT("This is the About dialog of the except sample.\n")
508 wxT("Welcome to %s"), wxVERSION_STRING
);
510 wxMessageBox(msg
, wxT("About Except"), wxOK
| wxICON_INFORMATION
, this);
513 // ============================================================================
514 // MyDialog implementation
515 // ============================================================================
517 MyDialog::MyDialog(wxFrame
*parent
)
518 : wxDialog(parent
, wxID_ANY
, wxString(wxT("Throw exception dialog")))
520 wxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
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);
531 SetSizerAndFit(sizerTop
);
534 void MyDialog::OnThrowInt(wxCommandEvent
& WXUNUSED(event
))
539 void MyDialog::OnThrowObject(wxCommandEvent
& WXUNUSED(event
))
541 throw MyException(wxT("Exception thrown from MyDialog"));
544 void MyDialog::OnCrash(wxCommandEvent
& WXUNUSED(event
))