๐ ๏ธ ASP.NET Error Handling & Debugging โ Catch, Trace, and Fix Web App Errors
๐งฒ Introduction โ Why Robust Error Handling Matters in ASP.NET
In any real-world web application, errors are inevitableโbut crashes donโt have to be. ASP.NET provides a comprehensive error-handling mechanism that allows developers to capture, log, and gracefully respond to unexpected failures. Coupled with modern debugging techniques, you can fix issues quickly and maintain user trust.
๐ฏ In this guide, youโll learn:
- How to handle runtime errors in ASP.NET (Web Forms & Core)
- How to use
try-catch,global.asax,customErrors, and middleware - How to debug ASP.NET apps using breakpoints, trace, and logs
- How to configure user-friendly error pages
๐ Topics Covered
| ๐น Topic | ๐ Description |
|---|---|
| โ ASP.NET โ Error Handling | Capture and manage application-level and page-level exceptions |
| ๐ ASP.NET โ Debugging | Use Visual Studio, Trace, and built-in tools to troubleshoot issues |
โ ASP.NET โ Error Handling
๐น Try-Catch Example (Code-Level)
try {
int result = 10 / int.Parse("0");
} catch (DivideByZeroException ex) {
lblError.Text = "Cannot divide by zero!";
}
โ Handles specific exceptions gracefully and prevents page crashes.
๐น Page-Level Error Handling
Use Page_Error() in code-behind:
protected void Page_Error(object sender, EventArgs e) {
Exception ex = Server.GetLastError();
Server.ClearError();
Response.Redirect("ErrorPage.aspx");
}
โ Great for redirecting users and logging details silently.
๐น Application-Level Errors (Global.asax)
void Application_Error(object sender, EventArgs e) {
Exception ex = Server.GetLastError();
// Log error to file or DB
Server.ClearError();
Response.Redirect("~/Error.aspx");
}
โ Centralized place to catch unhandled exceptions.
๐น Web.config Custom Errors
<customErrors mode="On" defaultRedirect="Error.aspx">
<error statusCode="404" redirect="NotFound.aspx" />
</customErrors>
| Mode | Behavior |
|---|---|
On | Custom errors shown to users |
Off | Full stack trace shown (for dev) |
RemoteOnly | Shown to remote users only |
๐น ASP.NET Core โ Error Handling Middleware
app.UseExceptionHandler("/Error");
In Error.cshtml, show a friendly message:
<h2>Oops! Something went wrong.</h2>
<p>Please try again later.</p>
โ
DeveloperExceptionPage shows full trace during development:
app.UseDeveloperExceptionPage();
๐ ASP.NET โ Debugging Techniques
๐ Debug with Visual Studio:
- โ Breakpoints: Pause execution at a specific line
- ๐ Watch/Locals: Inspect variable values
- ๐ Step Into/Over: Traverse logic flow
๐น Real-Time Trace Debugging:
<%@ Page Trace="true" %>
โ Appends trace information at the bottom of the page.
You can also enable in web.config:
<trace enabled="true" pageOutput="false" requestLimit="40" />
๐งพ Log to File or DB (Manual Debugging)
File.AppendAllText(Server.MapPath("~/logs/errors.txt"), ex.ToString());
โ
Useful in production when Trace is disabled.
๐ Remote Debugging in Production (Safe)
- Use tools like Application Insights, ELMAH, or Sentry
- Mask sensitive data in logs
- Avoid showing raw stack traces to users
๐ Summary โ Recap & Next Steps
With ASP.NETโs error handling and debugging tools, you can build resilient, user-friendly, and developer-friendly applications. Handle exceptions at every level, log intelligently, and debug interactively for faster troubleshooting.
๐ Key Takeaways:
- Use
try-catchto handle known error conditions - Configure
customErrorsfor user-friendly error pages - Use
Application_Erroror middleware for global exception logging - Debug using Visual Studio breakpoints, trace output, or log files
โ๏ธ Real-World Applications:
- Custom 404/500 pages to retain user experience
- Audit trail of errors for post-deployment analysis
- Error recovery flows for payment gateways, forms, and API calls
โ Frequently Asked Questions
โ What is the difference between customErrors and Application_Error?
โ
customErrors configures UI-level error redirection. Application_Error lets you handle and log exceptions globally in Global.asax.
โ Can I see detailed error messages on remote server?
โ
Set <customErrors mode="Off"/>, but only in developmentโnever expose stack traces in production.
โ Whatโs the best way to log errors in ASP.NET?
โ
Use System.Diagnostics.Trace, NLog, Serilog, or manual file/DB logging. ASP.NET Core supports built-in logging interfaces.
โ How to debug a deployed ASP.NET app?
โ
Use Application Insights, log files, or attach remote debugger using Visual Studio Enterprise.
Share Now :
