1 /////////////////////////////////////////////////////////////////////////////
2 // Name: samples/except/except.cpp
3 // Purpose: shows how C++ exceptions can be used in wxWidgets
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2003-2005 Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
28 #error "This sample only works with wxUSE_EXCEPTIONS == 1"
29 #endif // !wxUSE_EXCEPTIONS
31 // for all others, include the necessary headers (this file is usually all you
32 // need because it includes almost all "standard" wxWidgets headers)
38 #include "wx/dialog.h"
41 #include "wx/button.h"
45 #include "wx/msgdlg.h"
48 #include "wx/thread.h"
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 // the application icon (under Windows and OS/2 it is in resources)
56 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXX11__)
57 #include "../sample.xpm"
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
67 strcpy(p
, "Let's crash");
70 // ----------------------------------------------------------------------------
72 // ----------------------------------------------------------------------------
74 // Define a new application type, each program should derive a class from wxApp
75 class MyApp
: public wxApp
78 // override base class virtuals
79 // ----------------------------
82 virtual bool OnInit();
84 // 2nd-level exception handling: we get all the exceptions occurring in any
86 virtual bool OnExceptionInMainLoop();
88 // 3rd, and final, level exception handling: whenever an unhandled
89 // exception is caught, this function is called
90 virtual void OnUnhandledException();
92 // and now for something different: this function is called in case of a
93 // crash (e.g. dereferencing null pointer, division by 0, ...)
94 virtual void OnFatalException();
96 // you can override this function to do something different (e.g. log the
97 // assert to file) whenever an assertion fails
98 virtual void OnAssertFailure(const wxChar
*file
,
105 // Define a new frame type: this is going to be our main frame
106 class MyFrame
: public wxFrame
112 // event handlers (these functions should _not_ be virtual)
113 void OnQuit(wxCommandEvent
& event
);
114 void OnAbout(wxCommandEvent
& event
);
115 void OnDialog(wxCommandEvent
& event
);
117 void OnThrowInt(wxCommandEvent
& event
);
118 void OnThrowString(wxCommandEvent
& event
);
119 void OnThrowObject(wxCommandEvent
& event
);
120 void OnThrowUnhandled(wxCommandEvent
& event
);
122 void OnCrash(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
,
191 #if wxUSE_ON_FATAL_EXCEPTION
193 #endif // wxUSE_ON_FATAL_EXCEPTION
196 Except_ShowAssertInThread
,
197 #endif // wxUSE_THREADS
200 Except_Quit
= wxID_EXIT
,
201 Except_About
= wxID_ABOUT
204 // ----------------------------------------------------------------------------
205 // event tables and other macros for wxWidgets
206 // ----------------------------------------------------------------------------
208 // the event tables connect the wxWidgets events with the functions (event
209 // handlers) which process them. It can be also done at run-time, but for the
210 // simple menu events like this the static method is much simpler.
211 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
212 EVT_MENU(Except_Quit
, MyFrame::OnQuit
)
213 EVT_MENU(Except_About
, MyFrame::OnAbout
)
214 EVT_MENU(Except_Dialog
, MyFrame::OnDialog
)
215 EVT_MENU(Except_ThrowInt
, MyFrame::OnThrowInt
)
216 EVT_MENU(Except_ThrowString
, MyFrame::OnThrowString
)
217 EVT_MENU(Except_ThrowObject
, MyFrame::OnThrowObject
)
218 EVT_MENU(Except_ThrowUnhandled
, MyFrame::OnThrowUnhandled
)
219 EVT_MENU(Except_Crash
, MyFrame::OnCrash
)
220 #if wxUSE_ON_FATAL_EXCEPTION
221 EVT_MENU(Except_HandleCrash
, MyFrame::OnHandleCrash
)
222 #endif // wxUSE_ON_FATAL_EXCEPTION
223 EVT_MENU(Except_ShowAssert
, MyFrame::OnShowAssert
)
225 EVT_MENU(Except_ShowAssertInThread
, MyFrame::OnShowAssertInThread
)
226 #endif // wxUSE_THREADS
229 BEGIN_EVENT_TABLE(MyDialog
, wxDialog
)
230 EVT_BUTTON(Except_ThrowInt
, MyDialog::OnThrowInt
)
231 EVT_BUTTON(Except_ThrowObject
, MyDialog::OnThrowObject
)
232 EVT_BUTTON(Except_Crash
, MyDialog::OnCrash
)
235 // Create a new application object: this macro will allow wxWidgets to create
236 // the application object during program execution (it's better than using a
237 // static object for many reasons) and also implements the accessor function
238 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
242 // ============================================================================
243 // MyApp implementation
244 // ============================================================================
246 // 'Main program' equivalent: the program execution "starts" here
249 if ( !wxApp::OnInit() )
252 // create the main application window
253 MyFrame
*frame
= new MyFrame();
255 // and show it (the frames, unlike simple controls, are not shown when
256 // created initially)
259 // success: wxApp::OnRun() will be called which will enter the main message
260 // loop and the application will run. If we returned false here, the
261 // application would exit immediately.
265 bool MyApp::OnExceptionInMainLoop()
273 wxLogWarning(wxT("Caught an int %d in MyApp."), i
);
275 catch ( MyException
& e
)
277 wxLogWarning(wxT("Caught MyException(%s) in MyApp."), e
.what());
287 void MyApp::OnUnhandledException()
289 // this shows how we may let some exception propagate uncaught
294 catch ( UnhandledException
& )
300 wxMessageBox(wxT("Unhandled exception caught, program will terminate."),
301 wxT("wxExcept Sample"), wxOK
| wxICON_ERROR
);
305 void MyApp::OnFatalException()
307 wxMessageBox(wxT("Program has crashed and will terminate."),
308 wxT("wxExcept Sample"), wxOK
| wxICON_ERROR
);
311 void MyApp::OnAssertFailure(const wxChar
*file
,
317 // take care to not show the message box from a worker thread, this doesn't
318 // work as it doesn't have any event loop
319 if ( !wxIsMainThread() ||
322 wxString::Format("An assert failed in %s().", func
) +
324 "Do you want to call the default assert handler?",
326 wxYES_NO
| wxICON_QUESTION
329 wxApp::OnAssertFailure(file
, line
, func
, cond
, msg
);
333 // ============================================================================
334 // MyFrame implementation
335 // ============================================================================
339 : wxFrame(NULL
, wxID_ANY
, wxT("Except wxWidgets App"),
340 wxPoint(50, 50), wxSize(450, 340))
342 // set the frame icon
343 SetIcon(wxICON(sample
));
347 wxMenu
*menuFile
= new wxMenu
;
348 menuFile
->Append(Except_Dialog
, wxT("Show &dialog\tCtrl-D"));
349 menuFile
->AppendSeparator();
350 menuFile
->Append(Except_ThrowInt
, wxT("Throw an &int\tCtrl-I"));
351 menuFile
->Append(Except_ThrowString
, wxT("Throw a &string\tCtrl-S"));
352 menuFile
->Append(Except_ThrowObject
, wxT("Throw an &object\tCtrl-O"));
353 menuFile
->Append(Except_ThrowUnhandled
,
354 wxT("Throw &unhandled exception\tCtrl-U"));
355 menuFile
->Append(Except_Crash
, wxT("&Crash\tCtrl-C"));
356 menuFile
->AppendSeparator();
357 #if wxUSE_ON_FATAL_EXCEPTION
358 menuFile
->AppendCheckItem(Except_HandleCrash
, wxT("&Handle crashes\tCtrl-H"));
359 menuFile
->AppendSeparator();
360 #endif // wxUSE_ON_FATAL_EXCEPTION
361 menuFile
->Append(Except_ShowAssert
, wxT("Provoke &assert failure\tCtrl-A"));
363 menuFile
->Append(Except_ShowAssertInThread
,
364 wxT("Assert failure in &thread\tShift-Ctrl-A"));
365 #endif // wxUSE_THREADS
366 menuFile
->AppendSeparator();
367 menuFile
->Append(Except_Quit
, wxT("E&xit\tCtrl-Q"), wxT("Quit this program"));
369 wxMenu
*helpMenu
= new wxMenu
;
370 helpMenu
->Append(Except_About
, wxT("&About\tF1"), wxT("Show about dialog"));
372 // now append the freshly created menu to the menu bar...
373 wxMenuBar
*menuBar
= new wxMenuBar();
374 menuBar
->Append(menuFile
, wxT("&File"));
375 menuBar
->Append(helpMenu
, wxT("&Help"));
377 // ... and attach this menu bar to the frame
379 #endif // wxUSE_MENUS
381 #if wxUSE_STATUSBAR && !defined(__WXWINCE__)
382 // create a status bar just for fun (by default with 1 pane only)
384 SetStatusText(wxT("Welcome to wxWidgets!"));
385 #endif // wxUSE_STATUSBAR
388 bool MyFrame::ProcessEvent(wxEvent
& event
)
392 return wxFrame::ProcessEvent(event
);
394 catch ( const wxChar
*msg
)
396 wxLogMessage(wxT("Caught a string \"%s\" in MyFrame"), msg
);
402 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
404 // true is to force the frame to close
408 void MyFrame::OnDialog(wxCommandEvent
& WXUNUSED(event
))
418 wxLogWarning(wxT("An exception in MyDialog"));
425 void MyFrame::OnThrowInt(wxCommandEvent
& WXUNUSED(event
))
430 void MyFrame::OnThrowString(wxCommandEvent
& WXUNUSED(event
))
432 throw wxT("string thrown from MyFrame");
435 void MyFrame::OnThrowObject(wxCommandEvent
& WXUNUSED(event
))
437 throw MyException(wxT("Exception thrown from MyFrame"));
440 void MyFrame::OnThrowUnhandled(wxCommandEvent
& WXUNUSED(event
))
442 throw UnhandledException();
445 void MyFrame::OnCrash(wxCommandEvent
& WXUNUSED(event
))
450 #if wxUSE_ON_FATAL_EXCEPTION
452 void MyFrame::OnHandleCrash(wxCommandEvent
& event
)
454 wxHandleFatalExceptions(event
.IsChecked());
457 #endif // wxUSE_ON_FATAL_EXCEPTION
459 void MyFrame::OnShowAssert(wxCommandEvent
& WXUNUSED(event
))
461 // provoke an assert from wxArrayString
468 void MyFrame::OnShowAssertInThread(wxCommandEvent
& WXUNUSED(event
))
470 class AssertThread
: public wxThread
474 : wxThread(wxTHREAD_JOINABLE
)
479 virtual void *Entry()
481 wxFAIL_MSG("Test assert in another thread.");
493 #endif // wxUSE_THREADS
495 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
498 msg
.Printf( wxT("This is the About dialog of the except sample.\n")
499 wxT("Welcome to %s"), wxVERSION_STRING
);
501 wxMessageBox(msg
, wxT("About Except"), wxOK
| wxICON_INFORMATION
, this);
504 // ============================================================================
505 // MyDialog implementation
506 // ============================================================================
508 MyDialog::MyDialog(wxFrame
*parent
)
509 : wxDialog(parent
, wxID_ANY
, wxString(wxT("Throw exception dialog")))
511 wxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
513 sizerTop
->Add(new wxButton(this, Except_ThrowInt
, wxT("Throw &int")),
514 0, wxCENTRE
| wxALL
, 5);
515 sizerTop
->Add(new wxButton(this, Except_ThrowObject
, wxT("Throw &object")),
516 0, wxCENTRE
| wxALL
, 5);
517 sizerTop
->Add(new wxButton(this, Except_Crash
, wxT("&Crash")),
518 0, wxCENTRE
| wxALL
, 5);
519 sizerTop
->Add(new wxButton(this, wxID_CANCEL
, wxT("&Cancel")),
520 0, wxCENTRE
| wxALL
, 5);
522 SetSizerAndFit(sizerTop
);
525 void MyDialog::OnThrowInt(wxCommandEvent
& WXUNUSED(event
))
530 void MyDialog::OnThrowObject(wxCommandEvent
& WXUNUSED(event
))
532 throw MyException(wxT("Exception thrown from MyDialog"));
535 void MyDialog::OnCrash(wxCommandEvent
& WXUNUSED(event
))