.NET: How to convert Exception to string?

前端 未结 10 619
隐瞒了意图╮
隐瞒了意图╮ 2021-01-30 10:47

When an exception is thrown (while debugging in the IDE), i have the opportunity to view details of the exception:

10条回答
  •  礼貌的吻别
    2021-01-30 11:17

    There is no secret method. You could probably just override the ToString() method and build the string you want.

    Things like ErrorCode and Message are just properties of the exception that you can add to the desired string output.


    Update: After re-reading your question and thinking more about this, Jason's answer is more likely what you are wanting. Overriding the ToString() method would only be helpful for exceptions that you created, not already implemented ones. It doesn't make sense to sub class existing exceptions just to add this functionality.

提交回复
热议问题