Click here to Skip to main content
15,879,239 members
Articles / Web Development / ASP.NET
Article

Exception Handling in ASP.NET Applications

Rate me:
Please Sign up or sign in to vote.
2.65/5 (15 votes)
30 Jun 20054 min read 55.4K   34   6
Various ways of handling exceptions in ASP.NET applications.

Introduction

Need for Exception Handling

Exception handling is one of the core areas where a software architect needs to concentrate during SDLC cycle. The importance of exception handling comes in the entire cycle of SDLC. During design time, designers should be able to forecast the type of the errors that can occur in a particular component. During development, developers need to know the errors that are thrown due to a particular action that has been performed and should know how to rectify them. A tester is responsible for finding different types of errors that can occur as a result of different actions.

Hence exception handling is very much required for the proper flow of the application. Exception handling became vital in software due to the following reasons:

  • Frequent errors cause the user to quit the application.
  • Major errors take a lot of system resources.
  • Errors can affect the existing data if it is not handled properly.

Different ways of handling exceptions in Microsoft ASP.NET application are given below:

A. Using traditional try catch block

We can write our application and handle exceptions under the try catch blocks, which is the syntax of .NET. If you want to handle exception, then you can go for this approach. Otherwise you can write the try catch block in the event handlers in .aspx.cs file. The reason is if you don’t handle exception in any method, and if you are handling a method in its parent method, then the execution pointer will move to the error handling blocks of the parent method. Since event handlers are the parent method in a web scenario, we don’t have to handle errors in sub methods.

Pros:

  1. Developer can decide the exception handling algorithm.
  2. If required, each method can have a try catch method.
  3. Execution of the application even after the error has occurred is possible.

Cons:

  1. Handling algorithm may differ from developer to developer.
  2. Catching and throwing creates an overhead to the system.
  3. Application level handling is not possible.

B. Using Application_Error event handler

We can use the Application_Error event handler in Global.asax.cs file to handle application level errors. If an application error occurs, then the execution control will come to this event handler, and we can handle the error here. The typical method involves transferring the page to some custom error page and displaying the error method there. You can handle and log depending on your application requirements.

Pros:

  1. Transferring to another page is possible.
  2. Application level exception handling.

Cons:

  1. Execution of the application even after the error has occurred is not possible. It will be redirecting to the Error page, which is mentioned.
  2. New Web page design is needed.

C. Using CustomError tag in the Web.Config file

If you set the default redirect attribute of the custom error tag of the Web.Config file to any error handling page, then the execution control will be redirected to the URL mentioned. This is a good approach since we can handle the error in the custom error page.

Pros:

  1. Centralized page display.
  2. Able to use the advantages of the Web.Config file.

Cons:

  1. Server transfer is not possible.
  2. Execution of the application even after the error has occurred is not possible. It will be redirecting to the Error page, which is mentioned.
  3. New web page design is needed.

D. Using Enterprise Library Application Blocks

ELAB provides certain chunk of code compiled to a DLL which addresses the developer’s common problems while writing a code. This makes the developer’s work easier and hence the production time can be saved considerably. These are designed to encapsulate the Microsoft recommended best practices for .NET applications. They can be plugged into your .NET applications quickly and easily.

Some examples of ELAB are:

  1. Data Access Application Blocks.
  2. Exception Handling Application Blocks.
  3. Logging and Instrumentation Application Block.
  4. Caching Application Block.
  5. Configuration Application Block.
  6. Security Application Block.
  7. Cryptography Application Block.

Let us take Exception Handling Application Blocks for a case study.

Pros:

  1. Developer can reduce the number of lines of code to handle exception.
  2. No need for third party components which includes licensing and those type of stuffs.
  3. Excellent customizing options available.
  4. Can integrate with the logging facility and emailing.
  5. Simple to use (Add the reference to your application and start using EHAP).
  6. Feature ownership/class ownership can be given properly.

Cons:

  1. Customization requires developer effort.
  2. All the developers should understand the algorithm well.

We can use ELAB with other methods. For example, you can re-direct to an error page and there you can use ELAB. One Person can work on this error module and others can implement it after successful completion.

Useful links

Some useful links which explain Enterprise Library Application Blocks are given below:

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
Rakheesh C, working as a Tech Lead in a reputed MNC in Bangalore. Rakheesh is a post graduate in Computer Applications from Regional Engineering College, Calicut. His area of interests are Profiling in .NET Technologies, Web application performance and Memory Managements.

Comments and Discussions

 
GeneralException Handler Application Block Pin
Anonymous4-Jul-05 5:37
Anonymous4-Jul-05 5:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.