C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » ASP.NET/Web Applications »

Exception Handling in Asp.Net


Posted Date: 28 Oct 2008    Resource Type: Articles    Category: ASP.NET/Web Applications
Author: Pradeep IyerMember Level: Diamond    
Rating: 1 out of 5Points: 8



The various options available for us for handling errors in asp.net are:

• Code in such a way that possibility of error is less.
Try to avoid error conditions as far as possible
• Trapping errors as and when they occur.
You should be prepared to handle unexpected conditions like network failures, database crashes and the like.

public class TextReader
{
public string ReadLine()
{
try
{
// Read a line from the stream.
}
catch ( NullReferenceException NRE )
{
Response.Write("Null Reference Exception
");
}
catch ( Exception DefaultExcep )
{
Response.Write("Error Occured, Unknown Orgin");
}
finally
{
Response.Write("Caught Exception");
}
}
}


We can identify business error conditions in code itself rather than relaying on some database stored error codes.

We can provide much detailed and user-friendly information to the users.


public class TextReader
{
public string ReadLine()
{
try
{
// Read a line from the stream.
}
catch (Exception e)
{
throw new IOException ("Could not read from stream", e);
}
}
}


• Handle errors via Application_Error event in global.asax.
Application_Error event that gets fired every time an unhandled exception occurs in the web application.


protected void Application_Error(Object sender, EventArgs e)
{
Response.Write("Unexpected error occured ! <br>" &
Server.GetLastError().Message);
Response.End();
}


• Provide custom error pages for predefined IIS errors.
This provides much better user experience by the use of custom error pages.
The Page class has a property called as ErrorPage that can contain url of a HTML/ASPX page that will be displayed if any unhandled exception occurs in your code.
Mywebform.ErrorPage="errpage.aspx"
In the web.config file, turn on the customErrors mode as shown below:
<customErrors mode="On" />

• Provide custom error pages for business errors.
This you can customize error pages without going to IIS snap-in and also change them easily in future
Modify <customErrors> section of web.config file.


<customErrors mode="On" defaultRedirect="errpage.aspx">
<error statusCode="404" redirect="filenotfound.aspx" />
</customErrors>



REGARDS,
PRADEEP Y



Responses

Author: Gaurav Arora    28 Oct 2008Member Level: Diamond   Points : 2
Hi Paradeep,

A good article. Please note that:
Whenever you post an article, it should be written descriptive with ease of language.

Please visit Guidelines for more information.

This is a good Article. But it attracts reader with its more stuff if it can be as :

Intrduction:
Short Intro of article.
Moto:
Moto of Article
History:
History if any
Scope:
Scope of article
Description:
Detailed view of Article.
Standard:
Coding standard used in above.

Best of luck!

Thanks & regards,
Gaurav Arora



Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Exception Handling in .Net  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: DropDownList asp.net Control problems and challanges faced using appenddatabound items and autopostb
Previous Resource: Session / State Management in .Net
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET/Web Applications


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use