Application_Error handler isn't fired when use prefix async
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In ASP.NET, the Application_Error handler in Global.asax does not catch exceptions thrown inside async methods because asynchronous exceptions propagate differently than synchronous ones. When an async method throws, the exception is captured into the Task object. If nothing awaits that task or the exception is not observed, it bypasses the ASP.NET pipeline error handling and Application_Error never fires. The fix is to ensure all async methods are properly awaited, use exception filters or middleware (in ASP.NET Core), or add TaskScheduler.UnobservedTaskException as a safety net.
The Problem
Why Async Exceptions Bypass Application_Error
In synchronous ASP.NET, exceptions propagate up the call stack through the ASP.NET pipeline, which triggers Application_Error. In async code, exceptions are stored in the Task object. The ASP.NET pipeline for MVC 4 and Web API does not always unwrap these task exceptions into the standard error pipeline, so Application_Error is never called.
Fix 1: Use Exception Filters (ASP.NET MVC)
Exception filters work with both sync and async action methods in ASP.NET MVC.
Fix 2: Use ExceptionHandler (ASP.NET Web API)
Fix 3: ASP.NET Core Middleware (Recommended)
ASP.NET Core does not use Global.asax. Use middleware instead:
Fix 4: Catch Unobserved Task Exceptions
As a safety net for fire-and-forget tasks:
Fix 5: Wrap Async Calls Properly
Ensure all async paths are awaited:
ASP.NET Core Exception Handling Best Practice
Common Pitfalls
- Relying solely on
Application_Errorfor async error handling:Application_ErrorinGlobal.asaxwas designed for the synchronous ASP.NET pipeline. It does not reliably catch exceptions fromasynccontroller actions. Use exception filters (MVC),ExceptionHandler(Web API), or middleware (ASP.NET Core) instead. - Fire-and-forget async calls without error handling: Calling an async method without
awaitmeans its exception is never observed. The exception is silently swallowed (or triggersUnobservedTaskExceptionlater). Always await async calls, or wrap fire-and-forget calls in try-catch. - Using
async voidinstead ofasync Task:async voidmethods cannot be awaited, and their exceptions crash the process instead of being caught by the pipeline. Only useasync voidfor event handlers. Controller actions should always returnTask<ActionResult>. - Not registering exception filters globally: Exception filters registered only on specific controllers miss errors in other controllers. Register
GlobalExceptionFilterinFilterConfig(MVC) orWebApiConfig(Web API) to catch all unhandled exceptions. - Mixing ASP.NET Framework and Core patterns:
Global.asax,Application_Error, andHandleErrorAttributeare ASP.NET Framework concepts. ASP.NET Core usesUseExceptionHandler()middleware andIExceptionFilter. Migrating to Core requires replacing the entire error handling strategy, not just addingasync.
Summary
Application_Errordoes not catch async exceptions because they are captured in Task objects, not propagated through the ASP.NET pipeline- Use
IExceptionFilter(MVC) orExceptionHandler/ExceptionLogger(Web API) for ASP.NET Framework - Use
UseExceptionHandler()middleware in ASP.NET Core — it properly awaits and catches async exceptions - Always
awaitasync methods — fire-and-forget calls silently swallow exceptions - Use
TaskScheduler.UnobservedTaskExceptionas a last-resort safety net for unobserved task exceptions
Related reading
- Application window sent behind other windows on closing different thread C
- ArangoDB Synchronizing System Collections
- Are all distributed database designed to process data in parallel?
- Are all the web requests executed in parallel and handled asynchronously?
- application/json constant in .NET framework
- Apply function to all elements of collection through LINQ
- Application Loader stuck at Authenticating with the iTunes store when uploading an iOS app
- Application not picking up .css file flask/python

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.