简体   繁体   中英

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

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.

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 .

Try using .NET Reflector or similar to see what the ToString method on System.Exception is doing:

[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
public override string ToString()
{
    return this.ToString(true);
}

private string ToString(bool needFileLineInfo)
{
    string className;
    string message = this.Message;
    if ((message == null) || (message.Length <= 0))
    {
        className = this.GetClassName();
    }
    else
    {
        className = this.GetClassName() + ": " + message;
    }
    if (this._innerException != null)
    {
        className = className + " ---> " + this._innerException.ToString(needFileLineInfo) + Environment.NewLine + "   " + Environment.GetRuntimeResourceString("Exception_EndOfInnerExceptionStack");
    }
    string stackTrace = this.GetStackTrace(needFileLineInfo);
    if (stackTrace != null)
    {
        className = className + Environment.NewLine + stackTrace;
    }
    return className;
}

ToString() returns the Message along with the StackTrace .
ToString() will also recursively include InnerException s.

ToString() returns a much longer string which is much more useful than Message when tracking down errors.

You could always just try it and see:

try 
{
  throw new Exception("This is a test.");
}
catch ( Exception ex )
{
  Console.WriteLine(ex);
  Console.WriteLine(ex.Message);
}

(You will find that you are correct, .ToString is more informative, including among other things the stack trace.

The ToString methods returns the Message property along with information on where the error occured.

The Message property is intended for a short description of the error, and only contains what the person implementing the Exception put there. The resport from ToString contains additional information that is always included.

If you are running in debug mode, the error report contains more detailed information, eg line numbers in the call stack.

The e.ToString() will give you a detailed Message like PrintTrace i think which Display's the Exception Name and the Line where Exception was Thrown where e.Message Output's a Readable Message Only without Specification's.

You can check Exception base constructor

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM