]>
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 // Define a new frame type: this is going to be our main frame
96 class MyFrame
: public wxFrame
102 // event handlers (these functions should _not_ be virtual)
103 void OnQuit(wxCommandEvent
& event
);
104 void OnAbout(wxCommandEvent
& event
);
105 void OnDialog(wxCommandEvent
& event
);
107 void OnThrowInt(wxCommandEvent
& event
);
108 void OnThrowString(wxCommandEvent
& event
);
109 void OnThrowObject(wxCommandEvent
& event
);
110 void OnThrowUnhandled(wxCommandEvent
& event
);
112 void OnCrash(wxCommandEvent
& event
);
113 #if wxUSE_ON_FATAL_EXCEPTION
114 void OnHandleCrash(wxCommandEvent
& event
);
117 // 1st-level exception handling: we overload ProcessEvent() to be able to
118 // catch exceptions which occur in MyFrame methods here
119 virtual bool ProcessEvent(wxEvent
& event
);
122 // any class wishing to process wxWidgets events must use this macro
123 DECLARE_EVENT_TABLE()
126 // A simple dialog which has only some buttons to throw exceptions
127 class MyDialog
: public wxDialog
130 MyDialog(wxFrame
*parent
);
133 void OnThrowInt(wxCommandEvent
& event
);
134 void OnThrowObject(wxCommandEvent
& event
);
135 void OnCrash(wxCommandEvent
& event
);
138 DECLARE_EVENT_TABLE()
141 // A trivial exception class
145 MyException(const wxString
& msg
) : m_msg(msg
) { }
147 const wxChar
*what() const { return m_msg
.c_str(); }
153 // Another exception class which just has to be different from anything else
154 class UnhandledException
158 // ----------------------------------------------------------------------------
160 // ----------------------------------------------------------------------------
162 // IDs for the controls and the menu commands
165 // control ids and menu items
166 Except_ThrowInt
= wxID_HIGHEST
,
169 Except_ThrowUnhandled
,
171 #if wxUSE_ON_FATAL_EXCEPTION
176 Except_Quit
= wxID_EXIT
,
177 Except_About
= wxID_ABOUT
180 // ----------------------------------------------------------------------------
181 // event tables and other macros for wxWidgets
182 // ----------------------------------------------------------------------------
184 // the event tables connect the wxWidgets events with the functions (event
185 // handlers) which process them. It can be also done at run-time, but for the
186 // simple menu events like this the static method is much simpler.
187 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
188 EVT_MENU(Except_Quit
, MyFrame::OnQuit
)
189 EVT_MENU(Except_About
, MyFrame::OnAbout
)
190 EVT_MENU(Except_Dialog
, MyFrame::OnDialog
)
191 EVT_MENU(Except_ThrowInt
, MyFrame::OnThrowInt
)
192 EVT_MENU(Except_ThrowString
, MyFrame::OnThrowString
)
193 EVT_MENU(Except_ThrowObject
, MyFrame::OnThrowObject
)
194 EVT_MENU(Except_ThrowUnhandled
, MyFrame::OnThrowUnhandled
)
195 EVT_MENU(Except_Crash
, MyFrame::OnCrash
)
196 #if wxUSE_ON_FATAL_EXCEPTION
197 EVT_MENU(Except_HandleCrash
, MyFrame::OnHandleCrash
)
201 BEGIN_EVENT_TABLE(MyDialog
, wxDialog
)
202 EVT_BUTTON(Except_ThrowInt
, MyDialog::OnThrowInt
)
203 EVT_BUTTON(Except_ThrowObject
, MyDialog::OnThrowObject
)
204 EVT_BUTTON(Except_Crash
, MyDialog::OnCrash
)
207 // Create a new application object: this macro will allow wxWidgets to create
208 // the application object during program execution (it's better than using a
209 // static object for many reasons) and also implements the accessor function
210 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
214 // ============================================================================
215 // MyApp implementation
216 // ============================================================================
218 // 'Main program' equivalent: the program execution "starts" here
221 // create the main application window
222 MyFrame
*frame
= new MyFrame();
224 // and show it (the frames, unlike simple controls, are not shown when
225 // created initially)
228 // success: wxApp::OnRun() will be called which will enter the main message
229 // loop and the application will run. If we returned false here, the
230 // application would exit immediately.
234 bool MyApp::OnExceptionInMainLoop()
242 wxLogWarning(_T("Caught an int %d in MyApp."), i
);
244 catch ( MyException
& e
)
246 wxLogWarning(_T("Caught MyException(%s) in MyApp."), e
.what());
256 void MyApp::OnUnhandledException()
258 // this shows how we may let some exception propagate uncaught
263 catch ( UnhandledException
& )
269 wxMessageBox(_T("Unhandled exception caught, program will terminate."),
270 _T("wxExcept Sample"), wxOK
| wxICON_ERROR
);
274 void MyApp::OnFatalException()
276 wxMessageBox(_T("Program has crashed and will terminate."),
277 _T("wxExcept Sample"), wxOK
| wxICON_ERROR
);
280 // ============================================================================
281 // MyFrame implementation
282 // ============================================================================
286 : wxFrame(NULL
, wxID_ANY
, _T("Except wxWidgets App"),
287 wxPoint(50, 50), wxSize(450, 340))
289 // set the frame icon
290 SetIcon(wxICON(sample
));
294 wxMenu
*menuFile
= new wxMenu
;
295 menuFile
->Append(Except_Dialog
, _T("Show &dialog\tCtrl-D"));
296 menuFile
->AppendSeparator();
297 menuFile
->Append(Except_ThrowInt
, _T("Throw an &int\tCtrl-I"));
298 menuFile
->Append(Except_ThrowString
, _T("Throw a &string\tCtrl-S"));
299 menuFile
->Append(Except_ThrowObject
, _T("Throw an &object\tCtrl-O"));
300 menuFile
->Append(Except_ThrowUnhandled
,
301 _T("Throw &unhandled exception\tCtrl-U"));
302 menuFile
->Append(Except_Crash
, _T("&Crash\tCtrl-C"));
303 menuFile
->AppendSeparator();
304 #if wxUSE_ON_FATAL_EXCEPTION
305 menuFile
->AppendCheckItem(Except_HandleCrash
, _T("&Handle crashes\tCtrl-H"));
306 menuFile
->AppendSeparator();
308 menuFile
->Append(Except_Quit
, _T("E&xit\tCtrl-Q"), _T("Quit this program"));
310 wxMenu
*helpMenu
= new wxMenu
;
311 helpMenu
->Append(Except_About
, _T("&About...\tF1"), _T("Show about dialog"));
313 // now append the freshly created menu to the menu bar...
314 wxMenuBar
*menuBar
= new wxMenuBar();
315 menuBar
->Append(menuFile
, _T("&File"));
316 menuBar
->Append(helpMenu
, _T("&Help"));
318 // ... and attach this menu bar to the frame
320 #endif // wxUSE_MENUS
322 #if wxUSE_STATUSBAR && !defined(__WXWINCE__)
323 // create a status bar just for fun (by default with 1 pane only)
325 SetStatusText(_T("Welcome to wxWidgets!"));
326 #endif // wxUSE_STATUSBAR
329 bool MyFrame::ProcessEvent(wxEvent
& event
)
333 return wxFrame::ProcessEvent(event
);
335 catch ( const wxChar
*msg
)
337 wxLogMessage(_T("Caught a string \"%s\" in MyFrame"), msg
);
343 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
345 // true is to force the frame to close
349 void MyFrame::OnDialog(wxCommandEvent
& WXUNUSED(event
))
359 wxLogWarning(_T("An exception in MyDialog"));
366 void MyFrame::OnThrowInt(wxCommandEvent
& WXUNUSED(event
))
371 void MyFrame::OnThrowString(wxCommandEvent
& WXUNUSED(event
))
373 throw _T("string thrown from MyFrame");
376 void MyFrame::OnThrowObject(wxCommandEvent
& WXUNUSED(event
))
378 throw MyException(_T("Exception thrown from MyFrame"));
381 void MyFrame::OnThrowUnhandled(wxCommandEvent
& WXUNUSED(event
))
383 throw UnhandledException();
386 void MyFrame::OnCrash(wxCommandEvent
& WXUNUSED(event
))
391 #if wxUSE_ON_FATAL_EXCEPTION
393 void MyFrame::OnHandleCrash(wxCommandEvent
& event
)
395 wxHandleFatalExceptions(event
.IsChecked());
400 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
403 msg
.Printf( _T("This is the About dialog of the except sample.\n")
404 _T("Welcome to %s"), wxVERSION_STRING
);
406 wxMessageBox(msg
, _T("About Except"), wxOK
| wxICON_INFORMATION
, this);
409 // ============================================================================
410 // MyDialog implementation
411 // ============================================================================
413 MyDialog::MyDialog(wxFrame
*parent
)
414 : wxDialog(parent
, wxID_ANY
, wxString(_T("Throw exception dialog")))
416 wxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
418 sizerTop
->Add(new wxButton(this, Except_ThrowInt
, _T("Throw &int")),
419 0, wxCENTRE
| wxALL
, 5);
420 sizerTop
->Add(new wxButton(this, Except_ThrowObject
, _T("Throw &object")),
421 0, wxCENTRE
| wxALL
, 5);
422 sizerTop
->Add(new wxButton(this, Except_Crash
, _T("&Crash")),
423 0, wxCENTRE
| wxALL
, 5);
424 sizerTop
->Add(new wxButton(this, wxID_CANCEL
, _T("&Cancel")),
425 0, wxCENTRE
| wxALL
, 5);
431 void MyDialog::OnThrowInt(wxCommandEvent
& WXUNUSED(event
))
436 void MyDialog::OnThrowObject(wxCommandEvent
& WXUNUSED(event
))
438 throw MyException(_T("Exception thrown from MyDialog"));
441 void MyDialog::OnCrash(wxCommandEvent
& WXUNUSED(event
))