Introduction

     

Journeyman programmers know that proper error handling is critical to the operation of an application. Without it, your chances of making the application truly 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 handsomely as the project progresses. Yet, too often error handling is given short shrift, for want of time, interest, awareness, accessibility, or who knows what.

Fortunately, error handling has been greatly improved in ASP.NET, making it much more approachable and much easier to implement than was the case for classic ASP. Taking a page or two from the Java playbook , ASP.NET now 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 up 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 7.1 delves into this subject in some detail. It also helps you understand all the nuances of the Try...Catch...Finally block, and even includes sets of if...then solution statements and leading questions to help you choose how to properly implement error handling in a routine.


Page level

Recipe 7.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? The reason is that it allows you to handle all page-level errors in a uniform way, which can greatly simplify error-handling code and, at the same time, make it much 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 7.3 shows you how to handle at the application level the errors 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. It is our firm belief that 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 all about creating user-friendly error messages, which sounds simple enough but actually 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 then 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: 2006
Pages: 179

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