Thoughts on throwing and handling exceptions

    Based on my personal experience, I developed a certain concept of working with exceptions. This concept is “sharpened” for applications based on business processes. For system and other programs, it may be ineffective. Because I mainly use .Net, then examples of inclusions are given for the .Net platform.

    The concept is presented in the form of a table:
    Reason for throwing an exception Details Exclusion class (.Net platform) How to handle an exception
    1. Violation of the rules of the business process Violations of any internal rules of your system.

    For example, an attempt to withdraw funds from an account with a zero balance. Moreover, the rules can be flexible: for some types of accounts, you can be allowed to go negative.
    You must implement your class, the successor of Exception (previously recommended to inherit from ApplicationException, now changed).

    Typically, an exception contains user-friendly information.
    Process in accordance with the internal rules of your system.

    Most often, stop processing the current operation and issue a message to the user.

    You do not need to write these types of exceptions to the log file.
    2. Incorrect data 2.1. Incorrect user input. For example, a letter is entered instead of a number.If possible, handle without throwing an exception (a simple check or use TryParse). Otherwise, similar to 2.2.Prompt the user which data is not correct. Provide an opportunity to fix them.

    You do not need to write to the log file.
    2.2. Incorrect data from the data source (Web service, file, registry, database). For example, plain text is received instead of the XML format.There are no clear rules.

    FormatException, InvalidDataException, etc.

    Usually, a special exception class is introduced into .Net that occurs when data is incorrect. Examples: ConfigurationException, XmlException, SqlException, SqlTypeException, WebException. However, the occurrence of these exceptions does not necessarily mean that the problem is data incorrectness (there are other reasons).

    But ArgumentException, InvalidOperationException, etc., may well apply. This greatly complicates the handling of the exception.
    Two options: either the data provider violates the protocol, or the subscriber did not fully implement the processing of data according to the protocol.

    If the data provider is not adequate, you can substitute a “crutch”.

    If your “crutch” was able to solve the problem, you do not need to write to the log file. Otherwise, write the details to the log file and solve the problem at one of 2 levels.
    3. Invalid code 3.1. Breach of contract. IndexOutOfRangeException, NullReferenceException, AccessViolationException, ArgumentException, ArgumentNullException, ArgumentOutOfRangeException, InvalidCastException, etc. Write the details of the exception to the log file, send the log to the developer. Such exceptions are 100% the fault of the developer.

    Warn user about error. Depending on where the error occurred, decide whether to close the program (if the data may be damaged).

    Correct an error in the code based on the log data and update the program version.
    3.2. Violation of the invariant. InvalidOperationException
    4. System error (often due to a hardware problem).   Not removable: * ExecutionEngineException, StackOverflowException, OutOfMemoryException. In 99% of cases, it is not possible to handle these exceptions.In 99% of cases, do not process it at all. You can try to write the exception data to the log.

    I note that this structuring is based on basic concepts:

    1. Hardware.
    2. Software. In turn, software problems are divided into 2 subclasses: data problem and code problem.

    So, in principle, I did not come up with anything new.

    It is worth noting that errors in one category may cause exceptions in another category. For example, incorrect code may cause a StackOverflowException. Although, incorrect data should ideally not cause incorrect code errors or system errors.

    I would like to hear thoughts on the topic.

    Also popular now: