C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Error handling techniques in ASP.NET


Posted Date: 12 Mar 2004    Resource Type: Articles    Category: Web Applications
Author: KasiviswanathMember Level: Gold    
Rating: Points: 7



We can handle exceptions in ASP.NET using following methods.

1 Using Application_Error event
2 Using ErrorPage Property
3 Using Throw statement
4 Using Try...catch…finally

1. Using Application_Error event

We have global.asax file for every web application, that contains Application_Error event. This event gets fired when an unhandled exception occurs. We can create a log in this block of code.
Eg.

Protected void Application_Error(Object sender, EventArgs e)
{
Response.Write("Unexpected exception occured on (“ & Now & “) !
" + Server.GetLastError().Message);
Response.End();
}

We can get the details about the exception using Server.GetLastError().

2. Using Error Page

This property will help to display a common error page when unhandled errors occur.

Eg.
Open web.config file and turn on the customErrors mode as shown below:

<customErrors mode="On" /<


Add a web form named ErrPageDemo to the project and set its errorPage property to error.aspx Add a web form named error.aspx to the project
In the ErrPageDemo web form write code that can throw an exception. If any unhandled exception occurs automaticaly this error.aspx page will be displayed.

3. Using Throw Statement

Throw statement is useful when we are developing big and complex systems. You can create your own exception classes for the application; this will give uniform/user friendly in handling of error. You can throw exceptions using throw keyword.

Eg.

if (some_contition) then
throw new Exception("New Exception")
end if


4. Using Try...catch…finally

This is one of the best and common methods supported by Common Language Specification (CLS) for error handling. We can use this kind of error handling in any .Net language. This is one of the advantages of try/catch block.

Eg.

Try
[try statements ]

Catch [exception [As type] ] [ When expression ]
[catch statements ]
[Exit Try ]

[Finally [ finally statements ] ]

End Try




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

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: Writing File Contents into your output
Previous Resource: How to upload files in ASP.Net
Return to Discussion Resource Index
Post New Resource
Category: Web Applications


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use