Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats the difference between Exception's .ToString() and .Message?

Tags:

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.

like image 621
Brad8118 Avatar asked Sep 26 '11 14:09

Brad8118


People also ask

What does exception ToString do?

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.

What is e ToString in Java?

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)

How do I read an exception message in C#?

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.

Does exception ToString include inner 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.


1 Answers

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.

like image 54
Grant Winney Avatar answered Sep 20 '22 16:09

Grant Winney