Click here to Skip to main content
15,843,498 members
Articles / Web Development / ASP.NET

Global Exception Handling in ASP.NET

Rate me:
Please Sign up or sign in to vote.
3.35/5 (13 votes)
21 Feb 20022 min read 139.8K   1.3K   61   7
An article on handling errors in ASP.NET Applications

Sample Image


This text was based on the article "Global Exception Handling with ASP.NET" found at

So, you've just finished your Web Application using ASP.NET. Everythings seems to be working fine. But how to be notified if an error happens while the site is running? A solution to this problem is shown in this article.

Handling Errors in a ASP.NET application

First of all you must catch the unhandled exceptions that happen in your Web Application by handling the HttpApplication Error event. You accomplish this task by editing your Global.asax file as follow:

protected void Application_Error(object sender, EventArgs e)
	// excpt is the exception thrown
	// The exception that really happened is retrieved through the GetBaseException() method
	// because the exception returned by the GetLastError() is a HttpException
	Exception excpt = Server.GetLastError().GetBaseException();


How to be notified about those unhandled exceptions?

The 3 most common ways to be notified about those "programming mistakes" are: File Log, EventLog and Email. As you would imagine I wrote a simple class that does this notifing stuff according to your Web.Config settings. You can see below a sample Web.Config file.

<?xml version="1.0" encoding="utf-8" ?>

  <!-- application specific settings -->
	  	<!-- here you define the way you want to be notified -->
	  	<!-- LogEvent = 1, FileLog = 2 e Email = 4. Use 0 when you don't want to be notified -->
      	<add key="errorNotifier_NotifyMode" value="3" />

    	<!-- Here you define the event source entry in your LogEvent, the default value is ASP.NET App Error -->
      	<add key="errorNotifier_EventLogSource" value="Sample App error" />

     	<!-- Here you define the full path where the file logging should occur -->
     	<add key="errorNotifier_Filename" value="error.txt" />

     	<!-- The email address that will receive the notifications -->
     	<add key="errorNotifier_EmailTo" value="" />

     	<!-- The email account that sends the emails -->
     	<add key="errorNotifier_EmailFrom" value="" />

     	<!-- The email subject -->
     	<add key="errorNotifier_EmailSubject" value="Error in ASP.NET app" />

     	<!-- The SmtpServer address. Just use if you don't want to use the local SmtpServer -->
     	<add key="errorNotifier_EmailSmtpServer" value="mySmtpServerAddress" />



Now change the Application_Error event implementation in your Global.asax as something as:

protected void Application_Error(object sender, EventArgs e)
	XC.Web.ErrorHandler handler = new XC.Web.ErrorHandler();

	// if you wish you can change the settings by now
	// handler.EmailSettings.To = "";


You can also make you Web Application show a special page whenever an error happens, giving a better feedback to your users. To do it, edit your Web.Config file as follow:

	<customerrors mode="Off" defaultRedirect="~/myErrorPage.aspx" />

Possible values to mode:

  • On : you want to handle errors
  • RemoteOnly : the errors will be handled only when they happen in a box differents than the IIS's. That's useful for debugging
  • Off : you don't want to handle errors at all

The defaultRedirect value is the url that should be shown in case of a unhandled exception

It's important to note that the ASP.NET user must have the right to write to the EventLog in case this method of notification is used. Also it must have write permission to the directory of the log file, if apply.


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
Architect VisionOne AG
Switzerland Switzerland
XicoLoko is a brazilian developer based in Switzerland.

Comments and Discussions

QuestionHelp! I've not been able to handle this error! Pin
mnongkhlaw26-Jun-08 4:07
mnongkhlaw26-Jun-08 4:07 
GeneralPop-up Error Message in ASP.Net Pin
Member 59282830-Aug-03 19:52
Member 59282830-Aug-03 19:52 
GeneralPop-up Screen Pin
Member 59282830-Aug-03 19:50
Member 59282830-Aug-03 19:50 
GeneralPop-up Error Message in ASP.Net Pin
Member 59282830-Aug-03 19:49
Member 59282830-Aug-03 19:49 
May I ask whether do you know the codes to include if I want a error pop-up message window screen fpr prompting when the user keys in the wrong information? Please help!

GeneralRe: Pop-up Error Message in ASP.Net Pin
Chetan Ranpariya13-Dec-05 21:05
Chetan Ranpariya13-Dec-05 21:05 
GeneralEventLog Exception Pin
dvcpp31-Jul-03 5:16
dvcpp31-Jul-03 5:16 
GeneralRe: EventLog Exception Pin
robbierad15-Jul-04 17:42
robbierad15-Jul-04 17:42 

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.