Unhandled exceptions cause ASP.NET-based applications to quit unexpectedly in the .NET Framework

Gjelder: .NET Framework 4.5

Note This article applies to the Microsoft .NET Framework 2.0 and all later versions.


When an unhandled exception is thrown in a Microsoft ASP.NET-based application that is built on the Microsoft .NET Framework 2.0 or a later version, the application unexpectedly quits. When this problem occurs, no exception information that you must have to understanding the issue is logged in the Application log.

However, an event message that is similar to the following may be logged in the System log: Additionally, an event message that is similar to the following may be logged in the Application log:


This problem occurs because the default policy for unhandled exceptions has changed in the .NET Framework 2.0 and later versions. By default, the policy for unhandled exceptions is to end the worker process.

In the Microsoft .NET Framework 1.1 and in the Microsoft .NET Framework 1.0, unhandled exceptions on managed threads were ignored. Unless you attached a debugger to catch the exception, you would not realize that anything was wrong.

ASP.NET uses the default policy for unhandled exceptions in the .NET Framework 2.0 and later versions. When an unhandled exception is thrown, the ASP.NET-based application unexpectedly quits. 

This behavior does not apply to exceptions that occur in the context of a request. These kinds of exceptions are still handled and wrapped by an HttpException object. Exceptions that occur in the context of a request do not cause the worker process to end. However, unhandled exceptions outside the context of a request, such as exceptions on a timer thread or in a callback function, cause the worker process to end.


To resolve this problem, use one of the following methods.

Method 1

Modify the source code for the IHttpModule object so that it will log exception information to the Application log. The information that is logged will include the following:
  • The virtual directory path in which the exception occurred
  • The exception name
  • The message
  • The stack trace
To modify the IHttpModule object, follow these steps.

Note This code will log a message that has the Event Type of Error and the Event Source of ASP.NET 2.0.50727.0 in the Application log. To test the module, request an ASP.NET page that uses the ThreadPool.QueueUserWorkItem method to call a method that throws an unhandled exception.
  1. Put the following code in a file that is named UnhandledExceptionModule.cs.
    using System;
    using System.Diagnostics;
    using System.Globalization;
    using System.IO;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Threading;
    using System.Web;

    namespace WebMonitor {
    public class UnhandledExceptionModule: IHttpModule {

    static int _unhandledExceptionCount = 0;

    static string _sourceName = null;
    static object _initLock = new object();
    static bool _initialized = false;

    public void Init(HttpApplication app) {

    // Do this one time for each AppDomain.
    if (!_initialized) {
    lock (_initLock) {
    if (!_initialized) {

    string webenginePath = Path.Combine(RuntimeEnvironment.GetRuntimeDirectory(), "webengine.dll");

    if (!File.Exists(webenginePath)) {
    throw new Exception(String.Format(CultureInfo.InvariantCulture,
    "Failed to locate webengine.dll at '{0}'. This module requires .NET Framework 2.0.",

    FileVersionInfo ver = FileVersionInfo.GetVersionInfo(webenginePath);
    _sourceName = string.Format(CultureInfo.InvariantCulture, "ASP.NET {0}.{1}.{2}.0",
    ver.FileMajorPart, ver.FileMinorPart, ver.FileBuildPart);

    if (!EventLog.SourceExists(_sourceName)) {
    throw new Exception(String.Format(CultureInfo.InvariantCulture,
    "There is no EventLog source named '{0}'. This module requires .NET Framework 2.0.",

    AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(OnUnhandledException);

    _initialized = true;

    public void Dispose() {

    void OnUnhandledException(object o, UnhandledExceptionEventArgs e) {
    // Let this occur one time for each AppDomain.
    if (Interlocked.Exchange(ref _unhandledExceptionCount, 1) != 0)

    StringBuilder message = new StringBuilder("\r\n\r\nUnhandledException logged by UnhandledExceptionModule.dll:\r\n\r\nappId=");

    string appId = (string) AppDomain.CurrentDomain.GetData(".appId");
    if (appId != null) {

    Exception currentException = null;
    for (currentException = (Exception)e.ExceptionObject; currentException != null; currentException = currentException.InnerException) {

    EventLog Log = new EventLog();
    Log.Source = _sourceName;
    Log.WriteEntry(message.ToString(), EventLogEntryType.Error);

  2. Save the UnhandledExceptionModule.cs file to the following folder:
    C:\Program Files\Microsoft Visual Studio 8\VC
  3. Open the Microsoft Visual Studio 2005 Command Prompt.
  4. Type sn.exe -k key.snk, and then press ENTER.
  5. Type csc /t:library /r:system.web.dll,system.dll /keyfile:key.snk UnhandledExceptionModule.cs, and then press ENTER.
  6. Type gacutil.exe /if UnhandledExceptionModule.dll, and then press ENTER.
  7. Type ngen install UnhandledExceptionModule.dll, and then press ENTER.
  8. Type gacutil /l UnhandledExceptionModule, and then press ENTER to display the strong name for the UnhandledExceptionModule file.
  9. 9. Add the following code to the Web.config file of your ASP.NET-based application.
    <add name="UnhandledExceptionModule" 
    type="WebMonitor.UnhandledExceptionModule, <strong name>" />

Method 2

Change the unhandled exception policy back to the default behavior that occurs in the .NET Framework 1.1 and in the .NET Framework 1.0.

Note We do not recommend that you change the default behavior. If you ignore exceptions, the application may leak resources and abandon locks.

To enable this default behavior, add the following code to the Aspnet.config file that is located in the following folder:
<legacyUnhandledExceptionPolicy enabled="true" />


This behavior is by design.

More Information

For more information about changes in the .NET Framework 2.0, go to the following Microsoft Developer Network (MSDN) website: