C# equivalent to Java's Exception.printStackTrace()?

Epaga picture Epaga · Dec 2, 2008 · Viewed 52.7k times · Source

Is there a C# equivalent method to Java's Exception.printStackTrace() or do I have to write something myself, working my way through the InnerExceptions?

Answer

Drew Noakes picture Drew Noakes · Dec 2, 2008

Try this:

Console.WriteLine(ex.ToString());

From http://msdn.microsoft.com/en-us/library/system.exception.tostring.aspx:

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.

Note that in the above code the call to ToString isn't required as there's an overload that takes System.Object and calls ToString directly.