.NET: How to convert Exception to string?

前端 未结 10 718
隐瞒了意图╮
隐瞒了意图╮ 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 10:52

    For people who don't want to mess with overriding, this simple non-intrusive method might be enough:

        public static string GetExceptionDetails(Exception exception)
        {
            return "Exception: " + exception.GetType()
                + "\r\nInnerException: " + exception.InnerException
                + "\r\nMessage: " + exception.Message
                + "\r\nStackTrace: " + exception.StackTrace;
        }
    

    It does not show the SQLException-specific details you want, though...

提交回复
热议问题