Exception Handling 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 trycatch 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 trycatch 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:

Developer can decide the exception handling algorithm.

If required, each method can have a trycatch method.

Execution of the application even after the error has occurred is possible.

Cons:

Handling algorithm may differ from developer to developer.

Catching and throwing creates an overhead to the system.

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:

Transferring to another page is possible.

Application level exception handling.

Cons:

Execution of the application even after the error has occurred is not possible. It will be redirecting to the Error page, which is mentioned.

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:

Centralized page display.

Able to use the advantages of the Web.Config file.

Cons:

Server transfer is not possible.

Execution of the application even after the error has occurred is not possible. It will be redirecting to the Error page, which is mentioned.

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:

Data Access Application Blocks.

Exception Handling Application Blocks.

Logging and Instrumentation Application Block.

Caching Application Block.

Configuration Application Block.

Security Application Block.

Cryptography Application Block.

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

Pros:

Developer can reduce the number of lines of code to handle exception.

No need for third party components which includes licensing and those type of stuffs.

Excellent customizing options available.

Can integrate with the logging facility and emailing.

Simple to use (Add the reference to your application and start using EHAP).

Feature ownership/class ownership can be given properly.

Cons:

Customization requires developer effort.

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.

Share

About the Author

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.