8.0 Introduction


Journeyman programmers know that proper error handling is critical to the operation of an application. Without it, your chances of making the application fault tolerant are less than remote. Taking the time to plan an error-handling strategy in the early stages of a project can pay off as the project progresses. Yet, too often error handling is given short shrift, for want of time, interest, awareness, accessibility, and who knows what.

Fortunately, error handling has been greatly improved in ASP.NET, making it more approachable and easier to implement than classic ASP. Taking a page or two from the Java playbook, ASP.NET provides state-of-the-art handling of errors with exceptions and error-handler events.

The error-handling model in ASP.NET lets you handle errors easily at the method, page, and application levels of your web applications. Most applications will use some combination of these to handle problems when they arise. In this chapter, we have included recipes for handling errors at each level:


Method level

When does it make sense to handle errors locally versus letting them propagate to a higher level? In general, you want to handle recoverable errors in the method where they occur and let nonrecoverable errors propagate up. Recipe 8.1 details this subject. It helps you understand all the nuances of the try…Catch…Finally block and includes sets of if…then solution statements and leading questions to help you choose how to implement error handling in a routine.


Page level

Recipe 8.2 shows you how to trap errors in a page and redirect the user to another page. Why would you want to use this approach? It allows you to handle all page-level errors in a uniform way, which can simplify error-handling code and make it more consistent and robust. The trick is in keeping all the error-handling code in one place in the original page by leveraging the Page_Error method, as the recipe explains.


Application level

Recipe 8.3 shows you how to handle the errors at the application level that occur on any page of your application. The approach we advocate involves trapping errors that occur and logging them in an event log prior to redirecting the user to another page. By handling all exceptions at the application level, rather than at the method or page level, you can process all errors for the application in a single location. Doing all error handling in one place in an application is key to writing effective code. It requires understanding what happens to unhandled exceptions at the method, page, and application levels, which this recipe explains.

The final recipe is about creating user-friendly error messages, which sounds simple but involves creating a new exception class that inherits from the .NET Framework's base exception classes and adding the functionality your application requires. How to take advantage of this new class in the Catch block of your code is also explained.



ASP. NET Cookbook
ASP.Net 2.0 Cookbook (Cookbooks (OReilly))
ISBN: 0596100647
EAN: 2147483647
Year: 2003
Pages: 202

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net