Topic: ASP.NET

Cover image for product 047050367X
Robust ASP.NET Exception Handling
Lee Dumond
ISBN: 978-0-470-50367-6
Wrox Blox PDF
62 pages
May 2009
Electronic Version: US $6.99 Purchase This Download

About This Title  |  Download Code  |  Errata
Description
This Wrox Blox will teach you how to unravel the mysteries of exception handling in ASP.NET.   First, you’ll get a thorough introduction to structured exception handling in the .NET Framework, learn about the Exception class, the related C# language constructs, and how exceptions propagate up the call stack. Then, you’ll delve into a variety of practical topics such as: when, where, and how to properly throw, catch, and handle exceptions in your code; how to employ “defensive programming” techniques to avoid triggering the most common CLR exceptions; adopting exception handling “best practices,” handling exceptions from rich ASP.NET server controls, data-bound controls, data sources, and AJAX asynchronous postbacks; and processing unhandled exceptions at both the page and application levels. 

The author also walks you through the building of a complete, reusable, and fully configurable ASP.NET exception processing framework that extends far beyond ASP.NET’s built-in error handling capabilities. You’ll learn how to collect information about the state of the application when the exception occurred; how to leverage various ASP.NET state-handling mechanisms such as Application, Context, Session, ASP.NET Cache, cookies, and query strings to store and pass error data; create a friendly, informative, and highly functional custom error UI; show friendly error messages to “normal” visitors, while showing detailed error reports only to site managers based on authentication, authorization, or IP; provide a way for users to add their own information to error reports; persist errors to the Windows Event Log, XML files, or a custom database; send detailed error notifications via e-mail and SMS text; and build an administrative dashboard to list, sort, page, view, and manage error logs.

Once complete, you’ll learn how to quickly and easily add this new framework to any web site you create using a simple .NET assembly.   A complete sample application and C# source code for the exception processing framework are available as a companion download.  

Table of Contents

What Is an Exception? 2

The Exception Class 3

Structured Exception Handling 5

Handling Exceptions 5

Exception Propagation: Part 1 9

Throwing Exceptions 11

Exception Handling Best Practices 13

Defensive Programming 13

Choose the Right Exception to Throw 17

Exceptions Should Be Exceptional 18

Don’t Abuse Exceptions 18

Avoid Swallowing Exceptions 19

Don’t Try to Handle Everything 20

Other Exception Handling Scenarios 20

Handling Exceptions in ASP.NET Data Sources 21

Handling Exceptions in ASP.NET Data-Bound Controls 22

Handling Exceptions in ASP.NET Rich Server Controls 22

Handling Exceptions in Asynchronous Postbacks 24

Managing Unhandled Exceptions in ASP.NET 25

Exception Propagation: Part 2 25

The ASP.NET Default Exception Handler 28

Page.Error and HttpApplication.Error 30

Building a Custom ASP.NET Exception Handler 32

The “Fourth Coffee” Sample Site 33

The LD.ExceptionHandling Class Library 39

Creating a Custom Error UI 54

Adding the Custom Exception Handler to Your Own Site 56

Conclusion 57

Lee Dumond 58

Usage Rights for Wiley Wrox Blox  

Any Wrox Blox you purchase from this site will come with certain restrictions that allow Wiley to protect the copyrights of its products. After you purchase and download this title, you:    

  • Are entitled to three downloads
  • Are entitled to make a backup copy of the file for your own use
  • Are entitled to print the Wrox Blox for your own use
  • Are entitled to make annotations and comments in the Wrox Blox file for your own use
  • May not lend, sell or give the Wrox Blox to another user
  • May not place the Wrox Blox file on a network or any file sharing service for use by anyone other than yourself or allow anyone other than yourself to access it
  • May not copy the Wrox Blox file other than as allowed above
    May not copy, redistribute, or modify any portion of the Wrox Blox contents in any way without prior permission from Wiley

If you have any questions about these restrictions, you may contact Customer Care at (877) 762-2974 (8 a.m. - 5 p.m. EST, Monday - Friday). If you have any issues related to Technical Support, please contact us at 800-762-2974 (United States only) or 317-572-3994 (International) 8 a.m. - 8 p.m. EST, Monday - Friday).