Chapter 1. Robust ASP.NET Exception Handling

That does not compute.

— Julie Newmar as AF709, the robot from the U.S. television series My Living Doll, 1964

Most of you have probably seen old science fiction movies or television shows in which a computer, unable to process some irreconcilable paradox, utters a variation on the above phrase, often followed by some kind of meltdown or violent self-destruction. Thankfully, our web applications don't cause servers to belch smoke and flames when an error occurs!

In computer terms, an error is simply any condition that produces unwanted or unexpected results. A good strategy for dealing with potential errors is to use defensive programming techniques that help you avoid them. In part, this means proactively anticipating conditions under which an error might occur and writing code that prevents those conditions if possible. It also means catching errors as they occur and handling them if appropriate, perhaps by rerouting execution flow, setting alternative values, or displaying a helpful message to the user (hopefully something more informative than "does not compute").

Of course, keep in mind that you may not catch everything and that some errors are unavoidable or simply beyond your control. There may also be errors for which no sensible handling strategy exists. In those cases, you still need a way to process errors that remain unhandled. Part of doing that is to provide information that presents the appropriate level of detail to the user based on his or her status and allows him or her to take further action if necessary. Beyond that, you may want to notify interested parties that the error occurred and to log information about the error, as well as the state of the application when the error occurred, to a file, database, event log, or some other data store for further analysis. This helps site administrators identify potential issues and take actions that may prevent the error from happening in the future.

In this Wrox Blox, you'll learn how errors are treated in .NET applications — both in general and in ASP.NET web applications in particular. You'll learn how exceptions are used to signal the occurrence of an error and how exceptions travel through a program. You'll learn how you can handle exceptions in your code, how to throw exceptions from your code, and how to apply various best practices for working with exceptions. You'll also walk through the building of a custom enterprise-level exception-processing framework that provides robust error handling, logging, and notification services for a sample ASP.NET web site. Finally, you'll learn how you can easily reuse this framework in your own web sites.

We've got a lot of ground to cover, so let's get started.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.80.101