Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

General Exception Handling Strategy for .NET

I’m used to having try/catch blocks in every method. The reason for this is so that I can catch every exception at the point of infraction and log it. I understand, from my reading and conversations with others, that this isn’t a popular view. One should only catch what one is prepared to handle. However, if I don’t catch at the point of infraction, then it would be possible to never log that infraction and know about it. Note: When I do catch and don’t handle, I still throw. This allows me to let the exception propagate to something that will handle it, yet still let me log it at the point of infraction.

So... How does one avoid try/catch in every method, yet still log the error at the point at which it occurred?

like image 584
Bob Horn Avatar asked Jun 26 '09 17:06

Bob Horn


People also ask

What is exception handling in .NET framework?

Exception handling is a mechanism in which a programming construct is used to consistently trap, intercept and handle the error occurred during application execution.

What is the best practices for exception handling in C#?

The best practices for Exception Handling in C# are based on logging the exception. The log should be to logging library to keep a record of the exceptions. Log exceptions using log4net, NLog, and other frameworks used for the same purpose.

How do you handle exceptions in .NET core?

To handle exceptions and display user friendly messages, we need to install Microsoft. AspNetCore. Diagnostics NuGet package and add middleware in the Configure() method. If you are using Visual Studio templates to create ASP.NET Core application then this package might be already installed.


2 Answers

No, don't catch everything. Exceptions propagate higher up on the stack. All you have to do is make sure that the exception is caught before it gets to the top of the stack.

This means, for instance, that you should surround the code of an event handler with a try/catch block. An event handler may be the "top of the stack". Same for a ThreadStart handler or a callback from an asynchronous method.

You also want to catch exceptions on layer boundaries, though in that case, you might just want to wrap the exception in a layer-specific exception.

In the case of ASP.NET, you may decide to allow ASP.NET Health Monitoring to log the exception for you.

But you certainly don't ever need to catch exceptions in every method. That's a major anti-pattern. I would loudly object to you checking in code with that kind of exception handling.

like image 67
John Saunders Avatar answered Oct 21 '22 16:10

John Saunders


You can see everything at stack trace - no need to try/catch every method.

Stick to few rules:

  1. Use try/catch only if you want to use a custom exception type
  2. Define a new exception type only if upper levels needs to know that
  3. Try/catch at top level instead of doing that for each method
like image 37
Arnis Lapsa Avatar answered Oct 21 '22 14:10

Arnis Lapsa