What is the difference between ex.StackTrace vs ex.toString() for exceptions in C#/.NET? Does toString() include also the message and the StackTrace?
Asked
Active
Viewed 672 times
-8
-
5This 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
-
1Have 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 Answers
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 callingToString
on the inner exception, and the result of callingEnvironment.StackTrace
. If any of these members isnull
, 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