Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Exception handling in ASP.NET

Author: Mike Green

In this article, I will explain about exception and exception handing in ASP.NET.

 

Every developer has to deal with exceptions in his/her life. Especially we face lots of exceptions in our codes and application when we start our life as developer. Experienced developers could also face exceptions as there can be some invalid data input or any programming mistake. So every developer, if possible, has to avoid exceptions and if cannot avoid then handle exceptions in code.
 
What is Exception and Error?
 
Error and exception are often used interchangeably but there is a little difference in these terms. Error is unusual and unexpected condition that occurs in the execution of code. Error interrupts the normal flow of execution of code and in result generates an exception. In normal words we can say that an error is the event that means something wrong happened and an exception is the object generated by the event. In ASP.NET when an error occurs, an object of type exception is generated.
 
Some Exception objects in ASP.NET
 
DevideByZeroException
ArithmeticException
FileNotFoundException
IndexOutOfRangeException
OutOfMemoryException
InvalidCastException
InvalidOperationException
NullReferenceException
 
Exception Handling in ASP.NET
 
Exception handling is to handle errors and recover from the situation in a good manner. It is a technique for developers to deal with unwanted situations caused by errors. Exception handling is very important as errors sometimes can cause huge disaster to your application. Errors should be avoided in code so that there will be no exceptions generated by the code. But sometimes there is impossible or very difficult to avoid some errors. In that case exceptions should be handled in the code.
 
There are number of ways to handle exceptions in ASP.NET. Some people prefer to redirect users to an error page to show user friendly message. Some people use built-in mechanism of ASP.NET that is structured Exception handling.
 
Custom Exception Handling using Errors Pages
 
Whenever an error occurs and an exception generated, ASP.NET displays a default error page. It is said to be an unprofessional way to show default error pages to users. So you can redirect your users to custom error page when an error occurs. You can tell to your user in the page that what happened wrong or you can only display a user friendly message. Custom error pages can be specified in two different places.
 
Application Level (In web.config file)
 
You can specify custom error page in customErrors tag in web.cofg file. You have set its mode property on.
 
 
The user will be redirected to this page if any error occurs in your application.
 
Page Level (@Page directive)
 
You can also specify error page at individual page level in @Page directive. If you have specified your error page at application level, page level setting will override application level settings so the user will be redirect to the error page specified at page level. You have to set mode attribute of customError tag on in both application level and page level settings.
 
<%@ Page Language="C#" ErrorPage="~/ErrorPage.aspx" %>
 
Structured Exception handling
 
It is the most common and organized way of exception handling. It is build-in mechanism of .NET framework to handle exceptions.  It is try-catch-finally mechanism. If you expect that some part of the code may generate exception then put that code in a try block and put exception handling code in catch block so that your application does not crash because of that exception.
 
Try Block
 
We can write code in try block which you expect to generate any exception. It means we can write the code which will be tested for exceptions. Code that will open a connection with database or access a file can be in the try block.
 
Catch Block
 
One Catch block is must with a try block (if there is no finally block) because exception generated in try block will be handled in catch block. However ASP.NET provides flexibility to write multiple catch blocks for multiple exceptions. An exception will be handled in its particular catch block. So a catch block is associated with specific exception. A catch block which will handle a exception of a type will be written before a catch block that will handle its base type. So order of appearance in important when you have multiple catch blocks. We can put our exception recovering code in catch block.
 
Finally Block
 
Code which will be executed regardless of exception occurs or not.  You may skip finally block if you have written catch block with your try block. Code that will close a database connection or close a reader can be in the finally block.   
 
In C# you can write try-catch-finally blocks as:
 
try
{
    //write your code here that can generate exception
}
catch (Exception ex)
{
   //write your exception handling code here
}
finally
{
   //write your code which will always execute no matter exception is generated or not
}
 
In VB.NET you can write Try-Catch-Finally blocks as:
 
Try
   'write your code here that can generate exception
Catch ex As Exception
   'write your exception handling code here
Finally
   'write your code which will always execute no matter exception is generated or not

End Try 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to create watermark text in TextBox control using jQuery in ASP.NET

Disclaimer - Privacy
© 2002-2017 DevASP.net