]>
git.saurik.com Git - wxWidgets.git/blob - samples/except/except.cpp
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"
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
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"
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
65 strcpy(p
, "Let's crash");
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
72 // Define a new application type, each program should derive a class from wxApp
73 class MyApp
: public wxApp
76 // override base class virtuals
77 // ----------------------------
80 virtual bool OnInit();
82 // 2nd-level exception handling: we get all the exceptions occuring in any
84 virtual bool OnExceptionInMainLoop();
86 // 3rd, and final, level exception handling: whenever an unhandled
87 // exception is caught, this function is called
88 virtual void OnUnhandledException();
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();
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
,
101 #endif // __WXDEBUG__
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 #if wxUSE_ON_FATAL_EXCEPTION
123 void OnHandleCrash(wxCommandEvent
& event
);
126 // 1st-level exception handling: we overload ProcessEvent() to be able to
127 // catch exceptions which occur in MyFrame methods here
128 virtual bool ProcessEvent(wxEvent
& event
);
131 // show how an assert failure message box looks like
132 void OnShowAssert(wxCommandEvent
& event
);
133 #endif // __WXDEBUG__
136 // any class wishing to process wxWidgets events must use this macro
137 DECLARE_EVENT_TABLE()
140 // A simple dialog which has only some buttons to throw exceptions
141 class MyDialog
: public wxDialog
144 MyDialog(wxFrame
*parent
);
147 void OnThrowInt(wxCommandEvent
& event
);
148 void OnThrowObject(wxCommandEvent
& event
);
149 void OnCrash(wxCommandEvent
& event
);
152 DECLARE_EVENT_TABLE()
155 // A trivial exception class
159 MyException(const wxString
& msg
) : m_msg(msg
) { }
161 const wxChar
*what() const { return m_msg
.c_str(); }
167 // Another exception class which just has to be different from anything else
168 class UnhandledException
172 // ----------------------------------------------------------------------------
174 // ----------------------------------------------------------------------------
176 // IDs for the controls and the menu commands
179 // control ids and menu items
180 Except_ThrowInt
= wxID_HIGHEST
,
183 Except_ThrowUnhandled
,
185 #if wxUSE_ON_FATAL_EXCEPTION
187 #endif // wxUSE_ON_FATAL_EXCEPTION
190 #endif // __WXDEBUG__
193 Except_Quit
= wxID_EXIT
,
194 Except_About
= wxID_ABOUT
197 // ----------------------------------------------------------------------------
198 // event tables and other macros for wxWidgets
199 // ----------------------------------------------------------------------------
201 // the event tables connect the wxWidgets events with the functions (event
202 // handlers) which process them. It can be also done at run-time, but for the
203 // simple menu events like this the static method is much simpler.
204 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
205 EVT_MENU(Except_Quit
, MyFrame::OnQuit
)
206 EVT_MENU(Except_About
, MyFrame::OnAbout
)
207 EVT_MENU(Except_Dialog
, MyFrame::OnDialog
)
208 EVT_MENU(Except_ThrowInt
, MyFrame::OnThrowInt
)
209 EVT_MENU(Except_ThrowString
, MyFrame::OnThrowString
)
210 EVT_MENU(Except_ThrowObject
, MyFrame::OnThrowObject
)
211 EVT_MENU(Except_ThrowUnhandled
, MyFrame::OnThrowUnhandled
)
212 EVT_MENU(Except_Crash
, MyFrame::OnCrash
)
213 #if wxUSE_ON_FATAL_EXCEPTION
214 EVT_MENU(Except_HandleCrash
, MyFrame::OnHandleCrash
)
215 #endif // wxUSE_ON_FATAL_EXCEPTION
217 EVT_MENU(Except_ShowAssert
, MyFrame::OnShowAssert
)
218 #endif // __WXDEBUG__
221 BEGIN_EVENT_TABLE(MyDialog
, wxDialog
)
222 EVT_BUTTON(Except_ThrowInt
, MyDialog::OnThrowInt
)
223 EVT_BUTTON(Except_ThrowObject
, MyDialog::OnThrowObject
)
224 EVT_BUTTON(Except_Crash
, MyDialog::OnCrash
)
227 // Create a new application object: this macro will allow wxWidgets to create
228 // the application object during program execution (it's better than using a
229 // static object for many reasons) and also implements the accessor function
230 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
234 // ============================================================================
235 // MyApp implementation
236 // ============================================================================
238 // 'Main program' equivalent: the program execution "starts" here
241 // create the main application window
242 MyFrame
*frame
= new MyFrame();
244 // and show it (the frames, unlike simple controls, are not shown when
245 // created initially)
248 // success: wxApp::OnRun() will be called which will enter the main message
249 // loop and the application will run. If we returned false here, the
250 // application would exit immediately.
254 bool MyApp::OnExceptionInMainLoop()
262 wxLogWarning(_T("Caught an int %d in MyApp."), i
);
264 catch ( MyException
& e
)
266 wxLogWarning(_T("Caught MyException(%s) in MyApp."), e
.what());
276 void MyApp::OnUnhandledException()
278 // this shows how we may let some exception propagate uncaught
283 catch ( UnhandledException
& )
289 wxMessageBox(_T("Unhandled exception caught, program will terminate."),
290 _T("wxExcept Sample"), wxOK
| wxICON_ERROR
);
294 void MyApp::OnFatalException()
296 wxMessageBox(_T("Program has crashed and will terminate."),
297 _T("wxExcept Sample"), wxOK
| wxICON_ERROR
);
302 void MyApp::OnAssert(const wxChar
*file
,
307 // we don't have anything special to do here
308 wxApp::OnAssert(file
, line
, cond
, msg
);
311 #endif // __WXDEBUG__
313 // ============================================================================
314 // MyFrame implementation
315 // ============================================================================
319 : wxFrame(NULL
, wxID_ANY
, _T("Except wxWidgets App"),
320 wxPoint(50, 50), wxSize(450, 340))
322 // set the frame icon
323 SetIcon(wxICON(sample
));
327 wxMenu
*menuFile
= new wxMenu
;
328 menuFile
->Append(Except_Dialog
, _T("Show &dialog\tCtrl-D"));
329 menuFile
->AppendSeparator();
330 menuFile
->Append(Except_ThrowInt
, _T("Throw an &int\tCtrl-I"));
331 menuFile
->Append(Except_ThrowString
, _T("Throw a &string\tCtrl-S"));
332 menuFile
->Append(Except_ThrowObject
, _T("Throw an &object\tCtrl-O"));
333 menuFile
->Append(Except_ThrowUnhandled
,
334 _T("Throw &unhandled exception\tCtrl-U"));
335 menuFile
->Append(Except_Crash
, _T("&Crash\tCtrl-C"));
336 menuFile
->AppendSeparator();
337 #if wxUSE_ON_FATAL_EXCEPTION
338 menuFile
->AppendCheckItem(Except_HandleCrash
, _T("&Handle crashes\tCtrl-H"));
339 menuFile
->AppendSeparator();
340 #endif // wxUSE_ON_FATAL_EXCEPTION
342 menuFile
->Append(Except_ShowAssert
, _T("Provoke &assert failure\tCtrl-A"));
343 menuFile
->AppendSeparator();
344 #endif // __WXDEBUG__
345 menuFile
->Append(Except_Quit
, _T("E&xit\tCtrl-Q"), _T("Quit this program"));
347 wxMenu
*helpMenu
= new wxMenu
;
348 helpMenu
->Append(Except_About
, _T("&About...\tF1"), _T("Show about dialog"));
350 // now append the freshly created menu to the menu bar...
351 wxMenuBar
*menuBar
= new wxMenuBar();
352 menuBar
->Append(menuFile
, _T("&File"));
353 menuBar
->Append(helpMenu
, _T("&Help"));
355 // ... and attach this menu bar to the frame
357 #endif // wxUSE_MENUS
359 #if wxUSE_STATUSBAR && !defined(__WXWINCE__)
360 // create a status bar just for fun (by default with 1 pane only)
362 SetStatusText(_T("Welcome to wxWidgets!"));
363 #endif // wxUSE_STATUSBAR
366 bool MyFrame::ProcessEvent(wxEvent
& event
)
370 return wxFrame::ProcessEvent(event
);
372 catch ( const wxChar
*msg
)
374 wxLogMessage(_T("Caught a string \"%s\" in MyFrame"), msg
);
380 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
382 // true is to force the frame to close
386 void MyFrame::OnDialog(wxCommandEvent
& WXUNUSED(event
))
396 wxLogWarning(_T("An exception in MyDialog"));
403 void MyFrame::OnThrowInt(wxCommandEvent
& WXUNUSED(event
))
408 void MyFrame::OnThrowString(wxCommandEvent
& WXUNUSED(event
))
410 throw _T("string thrown from MyFrame");
413 void MyFrame::OnThrowObject(wxCommandEvent
& WXUNUSED(event
))
415 throw MyException(_T("Exception thrown from MyFrame"));
418 void MyFrame::OnThrowUnhandled(wxCommandEvent
& WXUNUSED(event
))
420 throw UnhandledException();
423 void MyFrame::OnCrash(wxCommandEvent
& WXUNUSED(event
))
428 #if wxUSE_ON_FATAL_EXCEPTION
430 void MyFrame::OnHandleCrash(wxCommandEvent
& event
)
432 wxHandleFatalExceptions(event
.IsChecked());
435 #endif // wxUSE_ON_FATAL_EXCEPTION
439 void MyFrame::OnShowAssert(wxCommandEvent
& WXUNUSED(event
))
441 // provoke an assert from wxArrayString
446 #endif // __WXDEBUG__
448 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
451 msg
.Printf( _T("This is the About dialog of the except sample.\n")
452 _T("Welcome to %s"), wxVERSION_STRING
);
454 wxMessageBox(msg
, _T("About Except"), wxOK
| wxICON_INFORMATION
, this);
457 // ============================================================================
458 // MyDialog implementation
459 // ============================================================================
461 MyDialog::MyDialog(wxFrame
*parent
)
462 : wxDialog(parent
, wxID_ANY
, wxString(_T("Throw exception dialog")))
464 wxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
466 sizerTop
->Add(new wxButton(this, Except_ThrowInt
, _T("Throw &int")),
467 0, wxCENTRE
| wxALL
, 5);
468 sizerTop
->Add(new wxButton(this, Except_ThrowObject
, _T("Throw &object")),
469 0, wxCENTRE
| wxALL
, 5);
470 sizerTop
->Add(new wxButton(this, Except_Crash
, _T("&Crash")),
471 0, wxCENTRE
| wxALL
, 5);
472 sizerTop
->Add(new wxButton(this, wxID_CANCEL
, _T("&Cancel")),
473 0, wxCENTRE
| wxALL
, 5);
479 void MyDialog::OnThrowInt(wxCommandEvent
& WXUNUSED(event
))
484 void MyDialog::OnThrowObject(wxCommandEvent
& WXUNUSED(event
))
486 throw MyException(_T("Exception thrown from MyDialog"));
489 void MyDialog::OnCrash(wxCommandEvent
& WXUNUSED(event
))