I'm looking through some code and I found e.ToString() and I was wondering if there is a difference to using the ToString() method instead of .Message ?
Reading below, it sounds like it returns more info.
From Microsoft's Docs
ToString Supported by the .NET Compact Framework. Overridden. Creates and returns a string representation of the current exception.
Message Supported by the .NET Compact Framework. Gets a message that describes the current exception.
ToString returns a representation of the current exception that is intended to be understood by humans. Where the exception contains culture-sensitive data, the string representation returned by ToString is required to take into account the current system culture.
Description. The java.lang.Throwable.toString() method returns a short description of this throwable. The result is the concatenation of − the name of the class of this object. ": " (a colon and a space)
To catch an exception that an async task throws, place the await expression in a try block, and catch the exception in a catch block. Uncomment the throw new Exception line in the example to demonstrate exception handling. The task's IsFaulted property is set to True , the task's Exception.
If you want information about all exceptions then use exception. ToString() . It will collect data from all inner exceptions. If you want only the original exception then use exception.
If you're looking to get as much information as possible in one go, call ToString()
:
The default implementation of ToString obtains the name of the class that threw the current exception, the message (my emphasis), the result of calling ToString on the inner exception, and the result of calling Environment.StackTrace. If any of these members is Nothing, its value is not included in the returned string.
It's convenient that you don't have to append all the individual elements together yourself, checking to make sure none are null, etc. It's all built in...
Exception.ToString Method
You can also check out the actual source code at reference.microsoft.com.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With