Exceptional throws: encapsulate them or not?

As soon as I read the MSDN article, which encouraged the following programming paradigm (its not 100% true ... see edit):

public class MyClass { public void Method1() { NewCustomException(); } public void Method2() { NewCustomException(); } void NewCustomException() { throw new CustomException("Exception message"); } } 

Do you think this paradigm makes sense? Wouldn't it be enough to store the exception message in the static const field and then pass it to the exception constructor instead of encapsulating the entire exception?

EDIT:

Use exception construction methods. it is common for a class to throw the same exception from different places in its implementation. To avoid excessive code, use helper methods that throw an exception and return it.

I just noticed (see the quote) that the article reports a return exception:

 public class MyClass { public void Method1() { throw NewCustomException(); } public void Method2() { throw NewCustomException(); } CustomException NewCustomException() { return new CustomException("Exception message"); } } 

What do you think about this?

+4
source share
7 answers

My understanding is that passing an exception instance is faux pas, if only for the reason that you lose the stack trace associated with the exception. Calling another method will change the stack trace and thereby make it useless. I would recommend, at a minimum, get the stack trace with the exception and pass it as an argument to some helper if you are going to go down this road.

+10
source

This refactoring is too far in my book. You must go back to the line in the stack trace to see exactly where the problem occurred. If your custom exception always uses the same message, put it in the CustomException class. If this is the same in the code you quoted, then yes, put it in the const field (you cannot have a static const - this is implicitly static ).

+4
source

Another problem you get is that there will be many places where you cannot even throw an exception because the compiler will not allow this. Consider these two methods added to your class:

  public string GetFoo1(bool bar) { if (bar) return ""; else NewCustomException(); } public string GetFoo2(bool bar) { if (bar) return ""; else throw new CustomException("Exception message"); } 

GetFoo1 will not compile while GetFoo2 will be.

+3
source

I will have a method that throws an exception, and not one that throws it. As in the example below. I seem to remember that Microsoft recommended it, but I can't remember where.

Using this technique, if you want to change the type of exception for any reason, you need to do it in only one place (for example, changing from ConfigurationException to ConfigurationErrorsException when upgrading from .NET 1.x to .NET 2.0).

You also respect the DRY principle by having a single copy of the code that creates the exception with its message and any other data included in the exception.

You obviously would not do this in trivial cases (for example, you would not replace throw new ArgumentNullException("myParamName") with throw BuildArgumentNullException("myParamName") )

 private static Exception BuildSomeException(... parameters with info to include in the exception ...) { string message = String.Format(...); return new SomeException(message, ...); } ... throw BuildSomeException(...); 
+2
source

I see no reason to make a method that just throws an exception. But, I think throwing custom exceptions is valuable. If all the exceptions that you throw are children of a custom exception, this allows you to quickly see if the exception that was thrown is the one you are considering, or something that you haven't handled yet. In addition, you can catch a MyBaseException , and it is not as bad as catching an Exception .

0
source

This is convenient if you do not know how you plan to handle exceptions. Do you want to just drop it? Or maybe later you are going to throw an exception somewhere and then throw it? Or maybe pass some arguments (i.e. method name, etc.) that come with the exception?

In this case, creating a separate method that handles the situation with an exception is convenient when you want to change it.

I usually don’t worry about this - instead, just think about how you are going to handle the exceptions (i.e. what line information you specify in the message).

0
source

I usually prefer to store message exceptions as resources. This serves several purposes:

  • If the requirement boils down to localizing exception messages, that is no problem.
  • Exception messages are generally more standardized for developers, as this is an extra step to create a new, but only slightly different message.
  • If you guarantee that the messages refer to the identifier, and include the identifier with an exception when it is thrown, then tracing the message to the code that threw it is easier.

Downside - it takes (just) a bit more effort than hard coding messages.

0
source

Source: https://habr.com/ru/post/1311762/


All Articles