]>
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 occurring 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
);
128 // 1st-level exception handling: we overload ProcessEvent() to be able to
129 // catch exceptions which occur in MyFrame methods here
130 virtual bool ProcessEvent(wxEvent
& event
);
133 // show how an assert failure message box looks like
134 void OnShowAssert(wxCommandEvent
& event
);
135 #endif // __WXDEBUG__
138 // any class wishing to process wxWidgets events must use this macro
139 DECLARE_EVENT_TABLE()
142 // A simple dialog which has only some buttons to throw exceptions
143 class MyDialog
: public wxDialog
146 MyDialog(wxFrame
*parent
);
149 void OnThrowInt(wxCommandEvent
& event
);
150 void OnThrowObject(wxCommandEvent
& event
);
151 void OnCrash(wxCommandEvent
& event
);
154 DECLARE_EVENT_TABLE()
157 // A trivial exception class
161 MyException(const wxString
& msg
) : m_msg(msg
) { }
163 const wxChar
*what() const { return m_msg
.c_str(); }
169 // Another exception class which just has to be different from anything else
170 class UnhandledException
174 // ----------------------------------------------------------------------------
176 // ----------------------------------------------------------------------------
178 // IDs for the controls and the menu commands
181 // control ids and menu items
182 Except_ThrowInt
= wxID_HIGHEST
,
185 Except_ThrowUnhandled
,
187 #if wxUSE_ON_FATAL_EXCEPTION
189 #endif // wxUSE_ON_FATAL_EXCEPTION
192 #endif // __WXDEBUG__
195 Except_Quit
= wxID_EXIT
,
196 Except_About
= wxID_ABOUT
199 // ----------------------------------------------------------------------------
200 // event tables and other macros for wxWidgets
201 // ----------------------------------------------------------------------------
203 // the event tables connect the wxWidgets events with the functions (event
204 // handlers) which process them. It can be also done at run-time, but for the
205 // simple menu events like this the static method is much simpler.
206 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
207 EVT_MENU(Except_Quit
, MyFrame::OnQuit
)
208 EVT_MENU(Except_About
, MyFrame::OnAbout
)
209 EVT_MENU(Except_Dialog
, MyFrame::OnDialog
)
210 EVT_MENU(Except_ThrowInt
, MyFrame::OnThrowInt
)
211 EVT_MENU(Except_ThrowString
, MyFrame::OnThrowString
)
212 EVT_MENU(Except_ThrowObject
, MyFrame::OnThrowObject
)
213 EVT_MENU(Except_ThrowUnhandled
, MyFrame::OnThrowUnhandled
)
214 EVT_MENU(Except_Crash
, MyFrame::OnCrash
)
215 #if wxUSE_ON_FATAL_EXCEPTION
216 EVT_MENU(Except_HandleCrash
, MyFrame::OnHandleCrash
)
217 #endif // wxUSE_ON_FATAL_EXCEPTION
219 EVT_MENU(Except_ShowAssert
, MyFrame::OnShowAssert
)
220 #endif // __WXDEBUG__
223 BEGIN_EVENT_TABLE(MyDialog
, wxDialog
)
224 EVT_BUTTON(Except_ThrowInt
, MyDialog::OnThrowInt
)
225 EVT_BUTTON(Except_ThrowObject
, MyDialog::OnThrowObject
)
226 EVT_BUTTON(Except_Crash
, MyDialog::OnCrash
)
229 // Create a new application object: this macro will allow wxWidgets to create
230 // the application object during program execution (it's better than using a
231 // static object for many reasons) and also implements the accessor function
232 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
236 // ============================================================================
237 // MyApp implementation
238 // ============================================================================
240 // 'Main program' equivalent: the program execution "starts" here
243 if ( !wxApp::OnInit() )
246 // create the main application window
247 MyFrame
*frame
= new MyFrame();
249 // and show it (the frames, unlike simple controls, are not shown when
250 // created initially)
253 // success: wxApp::OnRun() will be called which will enter the main message
254 // loop and the application will run. If we returned false here, the
255 // application would exit immediately.
259 bool MyApp::OnExceptionInMainLoop()
267 wxLogWarning(wxT("Caught an int %d in MyApp."), i
);
269 catch ( MyException
& e
)
271 wxLogWarning(wxT("Caught MyException(%s) in MyApp."), e
.what());
281 void MyApp::OnUnhandledException()
283 // this shows how we may let some exception propagate uncaught
288 catch ( UnhandledException
& )
294 wxMessageBox(wxT("Unhandled exception caught, program will terminate."),
295 wxT("wxExcept Sample"), wxOK
| wxICON_ERROR
);
299 void MyApp::OnFatalException()
301 wxMessageBox(wxT("Program has crashed and will terminate."),
302 wxT("wxExcept Sample"), wxOK
| wxICON_ERROR
);
307 void MyApp::OnAssert(const wxChar
*file
,
312 // we don't have anything special to do here
313 wxApp::OnAssert(file
, line
, cond
, msg
);
316 #endif // __WXDEBUG__
318 // ============================================================================
319 // MyFrame implementation
320 // ============================================================================
324 : wxFrame(NULL
, wxID_ANY
, wxT("Except wxWidgets App"),
325 wxPoint(50, 50), wxSize(450, 340))
327 // set the frame icon
328 SetIcon(wxICON(sample
));
332 wxMenu
*menuFile
= new wxMenu
;
333 menuFile
->Append(Except_Dialog
, wxT("Show &dialog\tCtrl-D"));
334 menuFile
->AppendSeparator();
335 menuFile
->Append(Except_ThrowInt
, wxT("Throw an &int\tCtrl-I"));
336 menuFile
->Append(Except_ThrowString
, wxT("Throw a &string\tCtrl-S"));
337 menuFile
->Append(Except_ThrowObject
, wxT("Throw an &object\tCtrl-O"));
338 menuFile
->Append(Except_ThrowUnhandled
,
339 wxT("Throw &unhandled exception\tCtrl-U"));
340 menuFile
->Append(Except_Crash
, wxT("&Crash\tCtrl-C"));
341 menuFile
->AppendSeparator();
342 #if wxUSE_ON_FATAL_EXCEPTION
343 menuFile
->AppendCheckItem(Except_HandleCrash
, wxT("&Handle crashes\tCtrl-H"));
344 menuFile
->AppendSeparator();
345 #endif // wxUSE_ON_FATAL_EXCEPTION
347 menuFile
->Append(Except_ShowAssert
, wxT("Provoke &assert failure\tCtrl-A"));
348 menuFile
->AppendSeparator();
349 #endif // __WXDEBUG__
350 menuFile
->Append(Except_Quit
, wxT("E&xit\tCtrl-Q"), wxT("Quit this program"));
352 wxMenu
*helpMenu
= new wxMenu
;
353 helpMenu
->Append(Except_About
, wxT("&About...\tF1"), wxT("Show about dialog"));
355 // now append the freshly created menu to the menu bar...
356 wxMenuBar
*menuBar
= new wxMenuBar();
357 menuBar
->Append(menuFile
, wxT("&File"));
358 menuBar
->Append(helpMenu
, wxT("&Help"));
360 // ... and attach this menu bar to the frame
362 #endif // wxUSE_MENUS
364 #if wxUSE_STATUSBAR && !defined(__WXWINCE__)
365 // create a status bar just for fun (by default with 1 pane only)
367 SetStatusText(wxT("Welcome to wxWidgets!"));
368 #endif // wxUSE_STATUSBAR
371 bool MyFrame::ProcessEvent(wxEvent
& event
)
375 return wxFrame::ProcessEvent(event
);
377 catch ( const wxChar
*msg
)
379 wxLogMessage(wxT("Caught a string \"%s\" in MyFrame"), msg
);
385 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
387 // true is to force the frame to close
391 void MyFrame::OnDialog(wxCommandEvent
& WXUNUSED(event
))
401 wxLogWarning(wxT("An exception in MyDialog"));
408 void MyFrame::OnThrowInt(wxCommandEvent
& WXUNUSED(event
))
413 void MyFrame::OnThrowString(wxCommandEvent
& WXUNUSED(event
))
415 throw wxT("string thrown from MyFrame");
418 void MyFrame::OnThrowObject(wxCommandEvent
& WXUNUSED(event
))
420 throw MyException(wxT("Exception thrown from MyFrame"));
423 void MyFrame::OnThrowUnhandled(wxCommandEvent
& WXUNUSED(event
))
425 throw UnhandledException();
428 void MyFrame::OnCrash(wxCommandEvent
& WXUNUSED(event
))
433 #if wxUSE_ON_FATAL_EXCEPTION
435 void MyFrame::OnHandleCrash(wxCommandEvent
& event
)
437 wxHandleFatalExceptions(event
.IsChecked());
440 #endif // wxUSE_ON_FATAL_EXCEPTION
444 void MyFrame::OnShowAssert(wxCommandEvent
& WXUNUSED(event
))
446 // provoke an assert from wxArrayString
451 #endif // __WXDEBUG__
453 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
456 msg
.Printf( wxT("This is the About dialog of the except sample.\n")
457 wxT("Welcome to %s"), wxVERSION_STRING
);
459 wxMessageBox(msg
, wxT("About Except"), wxOK
| wxICON_INFORMATION
, this);
462 // ============================================================================
463 // MyDialog implementation
464 // ============================================================================
466 MyDialog::MyDialog(wxFrame
*parent
)
467 : wxDialog(parent
, wxID_ANY
, wxString(wxT("Throw exception dialog")))
469 wxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
471 sizerTop
->Add(new wxButton(this, Except_ThrowInt
, wxT("Throw &int")),
472 0, wxCENTRE
| wxALL
, 5);
473 sizerTop
->Add(new wxButton(this, Except_ThrowObject
, wxT("Throw &object")),
474 0, wxCENTRE
| wxALL
, 5);
475 sizerTop
->Add(new wxButton(this, Except_Crash
, wxT("&Crash")),
476 0, wxCENTRE
| wxALL
, 5);
477 sizerTop
->Add(new wxButton(this, wxID_CANCEL
, wxT("&Cancel")),
478 0, wxCENTRE
| wxALL
, 5);
480 SetSizerAndFit(sizerTop
);
483 void MyDialog::OnThrowInt(wxCommandEvent
& WXUNUSED(event
))
488 void MyDialog::OnThrowObject(wxCommandEvent
& WXUNUSED(event
))
490 throw MyException(wxT("Exception thrown from MyDialog"));
493 void MyDialog::OnCrash(wxCommandEvent
& WXUNUSED(event
))