-8

What is the difference between ex.StackTrace vs ex.toString() for exceptions in C#/.NET? Does toString() include also the message and the StackTrace?

Lightsout
  • 3,454
  • 2
  • 36
  • 65
  • 5
    This could've easily been answerd by trying both and comparing. – madreflection Jan 27 '22 at 20:07
  • no because im asking about StackTrace not Message – Lightsout Jan 27 '22 at 20:15
  • 1
    Have you read the documents on [StackTrace](https://learn.microsoft.com/en-us/dotnet/api/system.exception.stacktrace?view=net-6.0) and [Exception.ToString](https://learn.microsoft.com/en-us/dotnet/api/system.exception.tostring?view=net-6.0)? If you want further clarification of the `Exception.ToString`, you could check out the [source](https://referencesource.microsoft.com/#mscorlib/system/exception.cs,430) of what it includes... – Trevor Jan 27 '22 at 20:24

1 Answers1

1

From the Exception.ToString documentation:

The default implementation of ToString obtains the name of the class that threw the current exception, the message, the result of calling ToString on the inner exception, and the result of calling Environment.StackTrace. If any of these members is null, its value is not included in the returned string.

If there is no error message or if it is an empty string (""), then no error message is returned. The name of the inner exception and the stack trace are returned only if they are not null.

Guru Stron
  • 102,774
  • 10
  • 95
  • 132