Why must a type thrown or caught derive from System.Exception

前端 未结 6 1005
独厮守ぢ
独厮守ぢ 2021-02-12 09:44

So just out of curiosity I wanted to see what was special about the exception class that allowed it to be used with the keyword Throw while a standard class is not.

6条回答
  •  爱一瞬间的悲伤
    2021-02-12 10:15

    The language uses System.Exception as a base for all exceptions. This essentially means that any throwable or catchable exception shouldn't error out if you do (Exception)myExc. This is probably because the definition of the System.Exception class is used so that all exceptions adhere to the same interface. Because of the consistent interface, exceptions arrive with a stack trace and a meaningful message (for example), which is invaluable for logging.

提交回复
热议问题