What is the difference between an error and an exception in .NET?

前端 未结 8 1916
说谎
说谎 2021-02-02 12:28

Could you please explain to me what the difference is between an error and an exception?

相关标签:
8条回答
  • 2021-02-02 13:19

    Exception: When a step in some action fails, all the subsequent steps in that action are simply NOT executed. This is where exceptions shine.

    Error: is when, like in the first case you want to halt execution of the current code, but before you do you need to free any resources previously allocated.


    Having that said,

    Exception class has HResult property. HRESULT is a 32-bit value, divided into three different fields: a severity code, a facility code, and an error code.

    Have a look at this post, will help you understand better

    • Error codes or Exceptions?
    0 讨论(0)
  • 2021-02-02 13:20

    Usually, I classify them as:

    Error - is a known workflow within the application. For example: Username not provided during authentication is an error.
    The application can handle these situation & will be able to show friendly messages to the user to prompt for proper input and/or process the data in a different.

    Exception - is usually throw when going out of your system and/or something unexpected happens in the application. For example: opening a file handle might throw an exception due to insufficient rights or the file not existing.
    Usually in this case, the application can catch these exception and/or write a generic handler to handle all the exceptions in the system.

    As a rule of thumb, if you know a particular case exists due to which the application cannot proceed working, label it as an error & handle the case gracefully.

    All remaining 'unknown-unknows' can then fall into the category of Exceptions.

    HTH.

    0 讨论(0)
提交回复
热议问题