Skip to content

Latest commit

 

History

History
70 lines (59 loc) · 2.06 KB

how-to-define-and-install-a-global-exception-handler.md

File metadata and controls

70 lines (59 loc) · 2.06 KB
descriptiontitlems.datehelpviewer_keywordsms.assetid
Learn more about: How to: Define and Install a Global Exception Handler
How to: Define and Install a Global Exception Handler
11/04/2016
handlers, global
dd88a812-3bc7-4ce8-8283-4b674c246534

How to: Define and Install a Global Exception Handler

The following code example demonstrates how unhandled exceptions can be captured. The example form contains a button that, when pressed, performs a null reference, causing an exception to be thrown. This functionality represents a typical code failure. The resulting exception is caught by the application-wide exception handler installed by the main function.

This is accomplished by binding a delegate to the xref:System.Windows.Forms.Application.ThreadException event. In this case, subsequent exceptions are then sent to the App::OnUnhandled method.

Example

// global_exception_handler.cpp// compile with: /clr #using <system.dll> #using <system.drawing.dll> #using <system.windows.forms.dll> usingnamespaceSystem;usingnamespaceSystem::Threading;usingnamespaceSystem::Drawing;usingnamespaceSystem::Windows::Forms; ref classMyForm : publicForm { Button^ b; public:MyForm( ) { b = gcnew Button( ); b->Text = "Do Null Access"; b->Size = Drawing::Size(150, 30); b->Click += gcnew EventHandler(this, &MyForm::OnClick); Controls->Add(b); } voidOnClick(Object^ sender, EventArgs^ args) { // do something illegal, like call through a null pointer... Object^ o = nullptr; o->ToString( ); } }; ref classApp { public:staticvoidOnUnhandled(Object^ sender, ThreadExceptionEventArgs^ e) { MessageBox::Show(e->Exception->Message, "Global Exeception"); Application::ExitThread( ); } }; intmain() { Application::ThreadException += gcnew ThreadExceptionEventHandler(App::OnUnhandled); MyForm^ form = gcnew MyForm( ); Application::Run(form); }

See also

Exception Handling

close