]> git.saurik.com Git - wxWidgets.git/blame - samples/except/except.cpp
Remove DoSetSizeHints() call from Create()
[wxWidgets.git] / samples / except / except.cpp
CommitLineData
556b8c1a 1/////////////////////////////////////////////////////////////////////////////
f78e4942
VZ
2// Name: samples/except/except.cpp
3// Purpose: shows how C++ exceptions can be used in wxWidgets
4// Author: Vadim Zeitlin
556b8c1a 5// Modified by:
f78e4942 6// Created: 2003-09-17
f78e4942 7// Copyright: (c) 2003-2005 Vadim Zeitlin
556b8c1a
VZ
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
11// ============================================================================
12// declarations
13// ============================================================================
14
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
18
19// For compilers that support precompilation, includes "wx/wx.h".
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
23 #pragma hdrstop
24#endif
25
f78e4942
VZ
26#if !wxUSE_EXCEPTIONS
27 #error "This sample only works with wxUSE_EXCEPTIONS == 1"
28#endif // !wxUSE_EXCEPTIONS
29
556b8c1a 30// for all others, include the necessary headers (this file is usually all you
be5a51fb 31// need because it includes almost all "standard" wxWidgets headers)
556b8c1a 32#ifndef WX_PRECOMP
532c7736
VZ
33 #include "wx/log.h"
34
556b8c1a
VZ
35 #include "wx/app.h"
36 #include "wx/frame.h"
37 #include "wx/dialog.h"
532c7736 38 #include "wx/menu.h"
556b8c1a
VZ
39
40 #include "wx/button.h"
41 #include "wx/sizer.h"
42
30deac1f
WS
43 #include "wx/utils.h"
44 #include "wx/msgdlg.h"
60c474a0 45 #include "wx/icon.h"
2d8e0096
VZ
46
47 #include "wx/thread.h"
556b8c1a
VZ
48#endif
49
50// ----------------------------------------------------------------------------
51// resources
52// ----------------------------------------------------------------------------
53
54// the application icon (under Windows and OS/2 it is in resources)
e7092398 55#ifndef wxHAS_IMAGES_IN_RESOURCES
556b8c1a
VZ
56 #include "../sample.xpm"
57#endif
58
cc9a3957
VZ
59// ----------------------------------------------------------------------------
60// private functions
61// ----------------------------------------------------------------------------
62
63static void DoCrash()
64{
65 char *p = 0;
66 strcpy(p, "Let's crash");
67}
68
556b8c1a
VZ
69// ----------------------------------------------------------------------------
70// private classes
71// ----------------------------------------------------------------------------
72
73// Define a new application type, each program should derive a class from wxApp
74class MyApp : public wxApp
75{
76public:
77 // override base class virtuals
78 // ----------------------------
79
80 // program startup
81 virtual bool OnInit();
82
3103e8a9 83 // 2nd-level exception handling: we get all the exceptions occurring in any
556b8c1a 84 // event handler here
cc9a3957 85 virtual bool OnExceptionInMainLoop();
556b8c1a
VZ
86
87 // 3rd, and final, level exception handling: whenever an unhandled
88 // exception is caught, this function is called
89 virtual void OnUnhandledException();
cc9a3957
VZ
90
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();
3eeb7e10 94
5cd3213b
VZ
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,
98 int line,
99 const wxChar *func,
100 const wxChar *cond,
101 const wxChar *msg);
556b8c1a
VZ
102};
103
104// Define a new frame type: this is going to be our main frame
105class MyFrame : public wxFrame
106{
107public:
108 // ctor(s)
cc9a3957 109 MyFrame();
556b8c1a
VZ
110
111 // event handlers (these functions should _not_ be virtual)
112 void OnQuit(wxCommandEvent& event);
113 void OnAbout(wxCommandEvent& event);
114 void OnDialog(wxCommandEvent& event);
cc9a3957
VZ
115
116 void OnThrowInt(wxCommandEvent& event);
556b8c1a 117 void OnThrowString(wxCommandEvent& event);
cc9a3957
VZ
118 void OnThrowObject(wxCommandEvent& event);
119 void OnThrowUnhandled(wxCommandEvent& event);
120
121 void OnCrash(wxCommandEvent& event);
55fd62c1 122 void OnTrap(wxCommandEvent& event);
b6b375a9 123#if wxUSE_ON_FATAL_EXCEPTION
cc9a3957 124 void OnHandleCrash(wxCommandEvent& event);
b6b375a9 125#endif
556b8c1a 126
004867db
FM
127protected:
128
556b8c1a
VZ
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);
132
2d8e0096
VZ
133 // provoke assert in main or worker thread
134 //
135 // this is used to show how an assert failure message box looks like
3eeb7e10 136 void OnShowAssert(wxCommandEvent& event);
2d8e0096
VZ
137#if wxUSE_THREADS
138 void OnShowAssertInThread(wxCommandEvent& event);
139#endif // wxUSE_THREADS
3eeb7e10 140
556b8c1a 141private:
be5a51fb 142 // any class wishing to process wxWidgets events must use this macro
556b8c1a
VZ
143 DECLARE_EVENT_TABLE()
144};
145
146// A simple dialog which has only some buttons to throw exceptions
147class MyDialog : public wxDialog
148{
149public:
150 MyDialog(wxFrame *parent);
151
152 // event handlers
153 void OnThrowInt(wxCommandEvent& event);
154 void OnThrowObject(wxCommandEvent& event);
f78e4942 155 void OnCrash(wxCommandEvent& event);
556b8c1a
VZ
156
157private:
158 DECLARE_EVENT_TABLE()
159};
160
161// A trivial exception class
162class MyException
163{
164public:
165 MyException(const wxString& msg) : m_msg(msg) { }
166
167 const wxChar *what() const { return m_msg.c_str(); }
168
169private:
170 wxString m_msg;
171};
172
cc9a3957
VZ
173// Another exception class which just has to be different from anything else
174class UnhandledException
175{
176};
177
556b8c1a
VZ
178// ----------------------------------------------------------------------------
179// constants
180// ----------------------------------------------------------------------------
181
182// IDs for the controls and the menu commands
183enum
184{
cc9a3957 185 // control ids and menu items
b6b375a9 186 Except_ThrowInt = wxID_HIGHEST,
cc9a3957 187 Except_ThrowString,
556b8c1a 188 Except_ThrowObject,
cc9a3957 189 Except_ThrowUnhandled,
f78e4942 190 Except_Crash,
55fd62c1 191 Except_Trap,
b6b375a9 192#if wxUSE_ON_FATAL_EXCEPTION
cc9a3957 193 Except_HandleCrash,
3eeb7e10 194#endif // wxUSE_ON_FATAL_EXCEPTION
3eeb7e10 195 Except_ShowAssert,
2d8e0096
VZ
196#if wxUSE_THREADS
197 Except_ShowAssertInThread,
198#endif // wxUSE_THREADS
556b8c1a
VZ
199 Except_Dialog,
200
201 Except_Quit = wxID_EXIT,
202 Except_About = wxID_ABOUT
203};
204
205// ----------------------------------------------------------------------------
be5a51fb 206// event tables and other macros for wxWidgets
556b8c1a
VZ
207// ----------------------------------------------------------------------------
208
be5a51fb 209// the event tables connect the wxWidgets events with the functions (event
556b8c1a
VZ
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.
212BEGIN_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)
cc9a3957 216 EVT_MENU(Except_ThrowInt, MyFrame::OnThrowInt)
556b8c1a 217 EVT_MENU(Except_ThrowString, MyFrame::OnThrowString)
cc9a3957
VZ
218 EVT_MENU(Except_ThrowObject, MyFrame::OnThrowObject)
219 EVT_MENU(Except_ThrowUnhandled, MyFrame::OnThrowUnhandled)
220 EVT_MENU(Except_Crash, MyFrame::OnCrash)
55fd62c1 221 EVT_MENU(Except_Trap, MyFrame::OnTrap)
b6b375a9 222#if wxUSE_ON_FATAL_EXCEPTION
cc9a3957 223 EVT_MENU(Except_HandleCrash, MyFrame::OnHandleCrash)
3eeb7e10 224#endif // wxUSE_ON_FATAL_EXCEPTION
3eeb7e10 225 EVT_MENU(Except_ShowAssert, MyFrame::OnShowAssert)
2d8e0096
VZ
226#if wxUSE_THREADS
227 EVT_MENU(Except_ShowAssertInThread, MyFrame::OnShowAssertInThread)
228#endif // wxUSE_THREADS
556b8c1a
VZ
229END_EVENT_TABLE()
230
231BEGIN_EVENT_TABLE(MyDialog, wxDialog)
232 EVT_BUTTON(Except_ThrowInt, MyDialog::OnThrowInt)
233 EVT_BUTTON(Except_ThrowObject, MyDialog::OnThrowObject)
f78e4942 234 EVT_BUTTON(Except_Crash, MyDialog::OnCrash)
556b8c1a
VZ
235END_EVENT_TABLE()
236
be5a51fb 237// Create a new application object: this macro will allow wxWidgets to create
556b8c1a
VZ
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
241// not wxApp)
242IMPLEMENT_APP(MyApp)
243
244// ============================================================================
245// MyApp implementation
246// ============================================================================
247
248// 'Main program' equivalent: the program execution "starts" here
249bool MyApp::OnInit()
250{
45e6e6f8
VZ
251 if ( !wxApp::OnInit() )
252 return false;
253
556b8c1a 254 // create the main application window
cc9a3957 255 MyFrame *frame = new MyFrame();
556b8c1a
VZ
256
257 // and show it (the frames, unlike simple controls, are not shown when
258 // created initially)
259 frame->Show(true);
260
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.
264 return true;
265}
266
cc9a3957 267bool MyApp::OnExceptionInMainLoop()
556b8c1a
VZ
268{
269 try
270 {
cc9a3957 271 throw;
556b8c1a
VZ
272 }
273 catch ( int i )
274 {
9a83f860 275 wxLogWarning(wxT("Caught an int %d in MyApp."), i);
556b8c1a 276 }
cc9a3957
VZ
277 catch ( MyException& e )
278 {
9a83f860 279 wxLogWarning(wxT("Caught MyException(%s) in MyApp."), e.what());
cc9a3957
VZ
280 }
281 catch ( ... )
282 {
283 throw;
284 }
285
286 return true;
556b8c1a
VZ
287}
288
289void MyApp::OnUnhandledException()
290{
cc9a3957
VZ
291 // this shows how we may let some exception propagate uncaught
292 try
293 {
294 throw;
295 }
296 catch ( UnhandledException& )
297 {
298 throw;
299 }
300 catch ( ... )
301 {
9a83f860
VZ
302 wxMessageBox(wxT("Unhandled exception caught, program will terminate."),
303 wxT("wxExcept Sample"), wxOK | wxICON_ERROR);
cc9a3957
VZ
304 }
305}
306
307void MyApp::OnFatalException()
308{
9a83f860
VZ
309 wxMessageBox(wxT("Program has crashed and will terminate."),
310 wxT("wxExcept Sample"), wxOK | wxICON_ERROR);
556b8c1a
VZ
311}
312
5cd3213b
VZ
313void MyApp::OnAssertFailure(const wxChar *file,
314 int line,
315 const wxChar *func,
316 const wxChar *cond,
317 const wxChar *msg)
3eeb7e10 318{
2d8e0096
VZ
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() ||
322 wxMessageBox
323 (
324 wxString::Format("An assert failed in %s().", func) +
325 "\n"
326 "Do you want to call the default assert handler?",
327 "wxExcept Sample",
328 wxYES_NO | wxICON_QUESTION
329 ) == wxYES )
5cd3213b
VZ
330 {
331 wxApp::OnAssertFailure(file, line, func, cond, msg);
332 }
3eeb7e10
VZ
333}
334
556b8c1a
VZ
335// ============================================================================
336// MyFrame implementation
337// ============================================================================
338
339// frame constructor
cc9a3957 340MyFrame::MyFrame()
9a83f860 341 : wxFrame(NULL, wxID_ANY, wxT("Except wxWidgets App"),
cc9a3957 342 wxPoint(50, 50), wxSize(450, 340))
556b8c1a
VZ
343{
344 // set the frame icon
d32b901b 345 SetIcon(wxICON(sample));
556b8c1a
VZ
346
347#if wxUSE_MENUS
348 // create a menu bar
349 wxMenu *menuFile = new wxMenu;
9a83f860 350 menuFile->Append(Except_Dialog, wxT("Show &dialog\tCtrl-D"));
cc9a3957 351 menuFile->AppendSeparator();
9a83f860
VZ
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"));
cc9a3957 355 menuFile->Append(Except_ThrowUnhandled,
9a83f860
VZ
356 wxT("Throw &unhandled exception\tCtrl-U"));
357 menuFile->Append(Except_Crash, wxT("&Crash\tCtrl-C"));
55fd62c1
VZ
358 menuFile->Append(Except_Trap, "&Trap\tCtrl-T",
359 "Break into the debugger (if one is running)");
cc9a3957 360 menuFile->AppendSeparator();
b6b375a9 361#if wxUSE_ON_FATAL_EXCEPTION
9a83f860 362 menuFile->AppendCheckItem(Except_HandleCrash, wxT("&Handle crashes\tCtrl-H"));
556b8c1a 363 menuFile->AppendSeparator();
3eeb7e10 364#endif // wxUSE_ON_FATAL_EXCEPTION
9a83f860 365 menuFile->Append(Except_ShowAssert, wxT("Provoke &assert failure\tCtrl-A"));
2d8e0096
VZ
366#if wxUSE_THREADS
367 menuFile->Append(Except_ShowAssertInThread,
368 wxT("Assert failure in &thread\tShift-Ctrl-A"));
369#endif // wxUSE_THREADS
3eeb7e10 370 menuFile->AppendSeparator();
9a83f860 371 menuFile->Append(Except_Quit, wxT("E&xit\tCtrl-Q"), wxT("Quit this program"));
556b8c1a
VZ
372
373 wxMenu *helpMenu = new wxMenu;
2d143b66 374 helpMenu->Append(Except_About, wxT("&About\tF1"), wxT("Show about dialog"));
556b8c1a
VZ
375
376 // now append the freshly created menu to the menu bar...
377 wxMenuBar *menuBar = new wxMenuBar();
9a83f860
VZ
378 menuBar->Append(menuFile, wxT("&File"));
379 menuBar->Append(helpMenu, wxT("&Help"));
556b8c1a
VZ
380
381 // ... and attach this menu bar to the frame
382 SetMenuBar(menuBar);
383#endif // wxUSE_MENUS
384
385#if wxUSE_STATUSBAR && !defined(__WXWINCE__)
386 // create a status bar just for fun (by default with 1 pane only)
387 CreateStatusBar(2);
9a83f860 388 SetStatusText(wxT("Welcome to wxWidgets!"));
556b8c1a
VZ
389#endif // wxUSE_STATUSBAR
390}
391
392bool MyFrame::ProcessEvent(wxEvent& event)
393{
394 try
395 {
396 return wxFrame::ProcessEvent(event);
397 }
398 catch ( const wxChar *msg )
399 {
9a83f860 400 wxLogMessage(wxT("Caught a string \"%s\" in MyFrame"), msg);
556b8c1a
VZ
401
402 return true;
403 }
404}
405
406void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
407{
408 // true is to force the frame to close
409 Close(true);
410}
411
412void MyFrame::OnDialog(wxCommandEvent& WXUNUSED(event))
413{
df5a063f
VZ
414 try
415 {
416 MyDialog dlg(this);
556b8c1a 417
df5a063f
VZ
418 dlg.ShowModal();
419 }
420 catch ( ... )
421 {
9a83f860 422 wxLogWarning(wxT("An exception in MyDialog"));
cc9a3957 423
df5a063f
VZ
424 Destroy();
425 throw;
426 }
556b8c1a
VZ
427}
428
cc9a3957
VZ
429void MyFrame::OnThrowInt(wxCommandEvent& WXUNUSED(event))
430{
431 throw -17;
432}
433
556b8c1a
VZ
434void MyFrame::OnThrowString(wxCommandEvent& WXUNUSED(event))
435{
9a83f860 436 throw wxT("string thrown from MyFrame");
cc9a3957
VZ
437}
438
439void MyFrame::OnThrowObject(wxCommandEvent& WXUNUSED(event))
440{
9a83f860 441 throw MyException(wxT("Exception thrown from MyFrame"));
cc9a3957
VZ
442}
443
444void MyFrame::OnThrowUnhandled(wxCommandEvent& WXUNUSED(event))
445{
446 throw UnhandledException();
447}
448
449void MyFrame::OnCrash(wxCommandEvent& WXUNUSED(event))
450{
451 DoCrash();
452}
453
55fd62c1
VZ
454void MyFrame::OnTrap(wxCommandEvent& WXUNUSED(event))
455{
456 wxTrap();
457}
458
b6b375a9
WS
459#if wxUSE_ON_FATAL_EXCEPTION
460
cc9a3957
VZ
461void MyFrame::OnHandleCrash(wxCommandEvent& event)
462{
463 wxHandleFatalExceptions(event.IsChecked());
556b8c1a
VZ
464}
465
3eeb7e10
VZ
466#endif // wxUSE_ON_FATAL_EXCEPTION
467
3eeb7e10
VZ
468void MyFrame::OnShowAssert(wxCommandEvent& WXUNUSED(event))
469{
470 // provoke an assert from wxArrayString
471 wxArrayString arr;
472 arr[0];
473}
474
2d8e0096
VZ
475#if wxUSE_THREADS
476
477void MyFrame::OnShowAssertInThread(wxCommandEvent& WXUNUSED(event))
478{
479 class AssertThread : public wxThread
480 {
481 public:
482 AssertThread()
483 : wxThread(wxTHREAD_JOINABLE)
484 {
485 }
486
487 protected:
488 virtual void *Entry()
489 {
490 wxFAIL_MSG("Test assert in another thread.");
491
492 return 0;
493 }
494 };
495
496 AssertThread thread;
497 thread.Create();
498 thread.Run();
499 thread.Wait();
500}
501
502#endif // wxUSE_THREADS
503
556b8c1a
VZ
504void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
505{
506 wxString msg;
9a83f860
VZ
507 msg.Printf( wxT("This is the About dialog of the except sample.\n")
508 wxT("Welcome to %s"), wxVERSION_STRING);
556b8c1a 509
9a83f860 510 wxMessageBox(msg, wxT("About Except"), wxOK | wxICON_INFORMATION, this);
556b8c1a
VZ
511}
512
513// ============================================================================
514// MyDialog implementation
515// ============================================================================
516
517MyDialog::MyDialog(wxFrame *parent)
9a83f860 518 : wxDialog(parent, wxID_ANY, wxString(wxT("Throw exception dialog")))
556b8c1a
VZ
519{
520 wxSizer *sizerTop = new wxBoxSizer(wxVERTICAL);
521
9a83f860 522 sizerTop->Add(new wxButton(this, Except_ThrowInt, wxT("Throw &int")),
556b8c1a 523 0, wxCENTRE | wxALL, 5);
9a83f860 524 sizerTop->Add(new wxButton(this, Except_ThrowObject, wxT("Throw &object")),
556b8c1a 525 0, wxCENTRE | wxALL, 5);
9a83f860 526 sizerTop->Add(new wxButton(this, Except_Crash, wxT("&Crash")),
f78e4942 527 0, wxCENTRE | wxALL, 5);
9a83f860 528 sizerTop->Add(new wxButton(this, wxID_CANCEL, wxT("&Cancel")),
556b8c1a
VZ
529 0, wxCENTRE | wxALL, 5);
530
92c01615 531 SetSizerAndFit(sizerTop);
556b8c1a
VZ
532}
533
534void MyDialog::OnThrowInt(wxCommandEvent& WXUNUSED(event))
535{
536 throw 17;
537}
538
539void MyDialog::OnThrowObject(wxCommandEvent& WXUNUSED(event))
540{
9a83f860 541 throw MyException(wxT("Exception thrown from MyDialog"));
556b8c1a
VZ
542}
543
f78e4942
VZ
544void MyDialog::OnCrash(wxCommandEvent& WXUNUSED(event))
545{
cc9a3957 546 DoCrash();
f78e4942
VZ
547}
548