Error and Exception Handling in Applications
In every application, sooner or later, there will come a situation where something goes wrong. It could be a missing file, a database connection problem, or sometimes a serious system failure. The key is how we respond to these problems — and this is where errors and exceptions come into play.
Errors vs. Exceptions — What's the Difference?
Error
This is a serious problem that the application usually should not or cannot fix during runtime.
Examples:
- System running out of memory
- Hardware failure
Exception
This is an unexpected situation, but one that the application can anticipate and handle.
Examples:
- Attempting to read a file that doesn't exist
- Invalid input data format
Why Is It Worth Handling Exceptions Properly?
- Increased application stability – users don't see sudden "Program has stopped working" messages
- Easier diagnostics – saved logs help find the source of the problem
- Lower repair costs – errors can be reproduced and fixed more quickly
Exception Handling Basics in C#
try
{
string content = File.ReadAllText("data.txt");
Console.WriteLine(content);
}
catch (FileNotFoundException ex)
{
Console.WriteLine("File not found.");
LogError(ex);
}
catch (Exception ex)
{
Console.WriteLine("An unexpected error occurred.");
LogError(ex);
}
void LogError(Exception ex)
{
// In a real application, write to file, database, or logging system like Sentry
Console.WriteLine($"[LOG] {DateTime.Now} - {ex.GetType()}: {ex.Message}");
}
Why Is This a Good Approach?
- Handling a specific exception (
FileNotFoundException
) provides better information to the user - General handling (
Exception
) catches remaining problems - Logging allows quick location of the error source
Best Practices
1. Only Catch Exceptions You Can Handle
Avoid empty catch { }
blocks – hiding problems makes diagnostics difficult.
2. Log Details
Date, exception type, message, call stack (StackTrace
).
3. Don't Use Exceptions for Logic Control
They are performance-expensive — use them only in exceptional situations.
4. Centralize Error Handling
In web applications, use a global handler (e.g., UseExceptionHandler
in ASP.NET).
5. Show Users Safe Messages
Technical details go to logs, while users get simple, understandable information.
Example of Global Exception Handling in ASP.NET Core
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.Use(async (context, next) =>
{
try
{
await next();
}
catch (Exception ex)
{
var logger = context.RequestServices.GetRequiredService<ILogger<Program>>();
logger.LogError(ex, "Unhandled exception occurred");
context.Response.StatusCode = 500;
await context.Response.WriteAsync("An error occurred while processing your request.");
}
});
}
This approach ensures that all unhandled exceptions are properly logged and users receive appropriate error messages without exposing sensitive technical details.