03.01.2015 Views

C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

216 ❘ CHAPTER 9 Error Handling<br />

Global Exception Handling<br />

Normally, you should try to catch an error as close as possible to the place where it occurs. An error<br />

is easiest to fix if you catch it right away in the method where it happens.<br />

However, bugs often arise in unexpected places. Unless you protect every method with error-handling<br />

code (a fairly common strategy), a bug may arise in code that you have not protected.<br />

Errors, Errors, Everywhere<br />

Some sources of errors are completely beyond your control. For example, power<br />

surges, static electricity, intermittent short circuits, or even stray radiation striking<br />

exactly the right part of a chip can make the computer’s hardware misbehave, so<br />

code that should work correctly fails. There’s little you can do to anticipate these<br />

kinds of errors but you can use global error handling to try to recover from them.<br />

Of course, that doesn’t excuse you from rigorously checking your code for errors.<br />

The vast majority of bugs are due to real mistakes in the code or data rather than to<br />

magical cosmic rays flipping a single bit on a memory chip.<br />

When you write a global exception handler, you probably won’t know where the exception will<br />

occur, so it’s hard to figure out how to keep the program running effectively. You can log the error,<br />

save data if necessary, and possibly display a message for the user before closing the program. You<br />

can make the program attempt to ignore the error and continue running, but it may be difficult to<br />

actually fix the problem.<br />

Let the User Escape<br />

If you decide to make the program ignore the error and keep running, display a<br />

message first and give the user a chance to close the program. Otherwise, the program<br />

might get stuck in an infinite loop where it ignores the error and then retries<br />

whatever operation caused the error in the first place.<br />

How you install a global error handler depends on the type of program you run. The following<br />

sections explain how to install global exception handlers for Windows Forms, WPF, and console<br />

applications.<br />

Note that global exception handlers don’t work inside Visual Studio. In all three kinds of programs,<br />

if the program throws an unhandled exception, Visual Studio springs into action and catches the<br />

error. To test the programs, you need to run a compiled executable program outside of Visual Studio.<br />

Windows Forms Applications<br />

To catch unhandled exceptions in a Windows Forms application, add an event handler to the<br />

Application object’s ThreadException event. The WindowsFormsGlobalException example<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!