Exclusion class: when to deduce from it, in C # (.Net)?

I am continuing to test my exam.

I came across using the Base Exception class, and I also saw it on the exam documents.

My question is when do you exit Base Exception class?

I get the impression that if you want a custom class to throw an exception from more relevant information, then you can create your own exception class that contains exact data that is representative of your custom class and the script is intended to be used for?

Why can't my own exception class be thrown from "ApplicationException" or "SecurityException" or the base class "Exception"?

I get the impression that I should have gotten the Exception base class, not the previous two.

My second question is when will you lose out of the other two ??? Are there any clear differences when you exit one of these three? Assuming there are no others that I have missed?

SMALL UPDATE:

This question from transcender pretty much hits the nail on the head.


* Which class should be used to create an exception for a specific application?

Answer: class ApplicationException *

+4
source share
4 answers

This is stated in the Design Guidelines .

+4
source

In our last project, we used the base exception class. We used it to get the following functions:

  • All exceptions required a number, so the property definition for the number was made in the base class
  • All exception messages should be formatted identically, with a number, reason, and type. This forms post was made in the base class.

Our base exception class comes from ApplicationException. Perhaps this was a mistake, there are many discussions about problems with too much inheritance. However, we did not have any problems with this.

One tip for the exam: Read the question carefully. Good luck.

+3
source

In general, you want to get from the Exception class that most closely resembles the type of exception you want to throw. If the problem is that an argument or parameter has been passed that causes the problem, use ArgumentException. If you need some kind of customization with this, inherit from ArgumentException.

In my experience, the only reasons for using a basic exception are: 1) when you need some kind of custom exception that does not fully correspond to one of the existing exception models, or 2) when a method can theoretically throw a number of exceptions, but you have already caught those you most likely you will quit.

As a rule, I don’t inherit exceptions at all. Just setting the Message property tends to be sufficient.

+2
source

Ideally, exceptions should be grouped in a hierarchy so that if the code handles several exceptions in the same way, they will all be derived from a common base class. If the base type, capable of throwing, was an interface, not a class, such an ideal might be somewhat achievable. However, the one-way restriction for classes severely limits the usefulness of the hierarchy.

The only time an exception hierarchy can be a useful concept is that implementing an interface or a new version of a class that is documented as throwing certain exceptions requires that the code be different from more different conditions than these exceptions. In such a scenario, an exception from the method of exceptions that is not the result of documented ones would be a violation of the changes, so you need to exclude the exception that inherits from the documented one that best describes the previously unforeseen condition. This is pretty ugly, but the exception handling mechanism doesn't really provide a better alternative. It’s very sad that things like IEnumerator<T>.MoveNext() are not documented as throwing any exceptions that would simply mean “Sorry - the system is not burning or something else, and I don’t know that anyone then changed the collection, but I can neither move on to the next item, nor honestly say that the transfer is completed, "but they do not.

Except when you need to create an exception that is compatible with existing code, it may be useful if the exceptions used by the application or library are derived from a common database. Instead of using ApplicationException it should be something like YourApplicationNameException or YourLibraryNameException - something from which nothing comes of it. Something like ApplicationException bad, because the code that catch ApplicationException will receive not only the exceptions it received from this type, but also any exceptions from which any other libraries are thrown.

0
source

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


All Articles