X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/cc9a39576ab6edb139c9bb77d01f4118853e04d2..27bb2c8cb78e39d8a4def1c349e0677fd32a2c06:/samples/except/except.cpp diff --git a/samples/except/except.cpp b/samples/except/except.cpp index 7d1c9991eb..95502800c7 100644 --- a/samples/except/except.cpp +++ b/samples/except/except.cpp @@ -79,7 +79,7 @@ public: // program startup virtual bool OnInit(); - // 2nd-level exception handling: we get all the exceptions occuring in any + // 2nd-level exception handling: we get all the exceptions occurring in any // event handler here virtual bool OnExceptionInMainLoop(); @@ -90,6 +90,15 @@ public: // and now for something different: this function is called in case of a // crash (e.g. dereferencing null pointer, division by 0, ...) virtual void OnFatalException(); + +#ifdef __WXDEBUG__ + // in debug mode, you can override this function to do something different + // (e.g. log the assert to file) whenever an assertion fails + virtual void OnAssert(const wxChar *file, + int line, + const wxChar *cond, + const wxChar *msg); +#endif // __WXDEBUG__ }; // Define a new frame type: this is going to be our main frame @@ -110,12 +119,21 @@ public: void OnThrowUnhandled(wxCommandEvent& event); void OnCrash(wxCommandEvent& event); +#if wxUSE_ON_FATAL_EXCEPTION void OnHandleCrash(wxCommandEvent& event); +#endif + +protected: // 1st-level exception handling: we overload ProcessEvent() to be able to // catch exceptions which occur in MyFrame methods here virtual bool ProcessEvent(wxEvent& event); +#ifdef __WXDEBUG__ + // show how an assert failure message box looks like + void OnShowAssert(wxCommandEvent& event); +#endif // __WXDEBUG__ + private: // any class wishing to process wxWidgets events must use this macro DECLARE_EVENT_TABLE() @@ -161,12 +179,17 @@ class UnhandledException enum { // control ids and menu items - Except_ThrowInt = 100, + Except_ThrowInt = wxID_HIGHEST, Except_ThrowString, Except_ThrowObject, Except_ThrowUnhandled, Except_Crash, +#if wxUSE_ON_FATAL_EXCEPTION Except_HandleCrash, +#endif // wxUSE_ON_FATAL_EXCEPTION +#ifdef __WXDEBUG__ + Except_ShowAssert, +#endif // __WXDEBUG__ Except_Dialog, Except_Quit = wxID_EXIT, @@ -189,7 +212,12 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_MENU(Except_ThrowObject, MyFrame::OnThrowObject) EVT_MENU(Except_ThrowUnhandled, MyFrame::OnThrowUnhandled) EVT_MENU(Except_Crash, MyFrame::OnCrash) +#if wxUSE_ON_FATAL_EXCEPTION EVT_MENU(Except_HandleCrash, MyFrame::OnHandleCrash) +#endif // wxUSE_ON_FATAL_EXCEPTION +#ifdef __WXDEBUG__ + EVT_MENU(Except_ShowAssert, MyFrame::OnShowAssert) +#endif // __WXDEBUG__ END_EVENT_TABLE() BEGIN_EVENT_TABLE(MyDialog, wxDialog) @@ -212,6 +240,9 @@ IMPLEMENT_APP(MyApp) // 'Main program' equivalent: the program execution "starts" here bool MyApp::OnInit() { + if ( !wxApp::OnInit() ) + return false; + // create the main application window MyFrame *frame = new MyFrame(); @@ -271,6 +302,19 @@ void MyApp::OnFatalException() _T("wxExcept Sample"), wxOK | wxICON_ERROR); } +#ifdef __WXDEBUG__ + +void MyApp::OnAssert(const wxChar *file, + int line, + const wxChar *cond, + const wxChar *msg) +{ + // we don't have anything special to do here + wxApp::OnAssert(file, line, cond, msg); +} + +#endif // __WXDEBUG__ + // ============================================================================ // MyFrame implementation // ============================================================================ @@ -295,8 +339,14 @@ MyFrame::MyFrame() _T("Throw &unhandled exception\tCtrl-U")); menuFile->Append(Except_Crash, _T("&Crash\tCtrl-C")); menuFile->AppendSeparator(); +#if wxUSE_ON_FATAL_EXCEPTION menuFile->AppendCheckItem(Except_HandleCrash, _T("&Handle crashes\tCtrl-H")); menuFile->AppendSeparator(); +#endif // wxUSE_ON_FATAL_EXCEPTION +#ifdef __WXDEBUG__ + menuFile->Append(Except_ShowAssert, _T("Provoke &assert failure\tCtrl-A")); + menuFile->AppendSeparator(); +#endif // __WXDEBUG__ menuFile->Append(Except_Quit, _T("E&xit\tCtrl-Q"), _T("Quit this program")); wxMenu *helpMenu = new wxMenu; @@ -380,11 +430,26 @@ void MyFrame::OnCrash(wxCommandEvent& WXUNUSED(event)) DoCrash(); } +#if wxUSE_ON_FATAL_EXCEPTION + void MyFrame::OnHandleCrash(wxCommandEvent& event) { wxHandleFatalExceptions(event.IsChecked()); } +#endif // wxUSE_ON_FATAL_EXCEPTION + +#ifdef __WXDEBUG__ + +void MyFrame::OnShowAssert(wxCommandEvent& WXUNUSED(event)) +{ + // provoke an assert from wxArrayString + wxArrayString arr; + arr[0]; +} + +#endif // __WXDEBUG__ + void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) { wxString msg; @@ -412,8 +477,7 @@ MyDialog::MyDialog(wxFrame *parent) sizerTop->Add(new wxButton(this, wxID_CANCEL, _T("&Cancel")), 0, wxCENTRE | wxALL, 5); - SetSizer(sizerTop); - sizerTop->Fit(this); + SetSizerAndFit(sizerTop); } void MyDialog::OnThrowInt(wxCommandEvent& WXUNUSED(event))