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

前端 未结 8 1877
说谎
说谎 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:02

    An exception is an object of a type deriving from the System.Exception class. It is used in a throw statement to transfer control to a catch clause in a try block somewhere further up the call stack.

    An error is just some code or message that you're meant to interpret. The problem with error codes is that you can decide to ignore them:

    MethodThatReturnsAnError();
    SomeCodeThatShouldNotExecuteOnError();
    

    That call will simply ignore the error code if one is returned. However:

    MethodThatThrowsAnException();
    SomeCodeThatShouldNotExecuteOnError();
    

    This cannot be ignored, and will transfer control up the stack, past "SomeCodeThatShouldNotExecuteOnError();".

提交回复
热议问题