25

Is there any API that allows to prints all the exception-related info (stack trace, inner etc...)? Just like when the exception is thrown - all the data is printed to the standard output - is there any dedicated method that does it all?

thanks

BreakPhreak
  • 9,960
  • 25
  • 69
  • 107
  • You want to physically print it out, like on paper? Or do you want to log it somewhere with the detail level you see in standard output? – curtisk Dec 15 '10 at 13:54
  • 10
    @curtisk The day I want to print out an exception's stack trace on paper is the day I quit programming – Archimaredes May 31 '16 at 16:14
  • Related: https://stackoverflow.com/questions/5928976/what-is-the-proper-way-to-display-the-full-innerexception – nawfal Aug 27 '18 at 15:41

4 Answers4

49
Console.WriteLine(exception.ToString());
jgauffin
  • 97,689
  • 42
  • 231
  • 359
7

the ToString method of Exception does exactly that.

Klaus Byskov Pedersen
  • 111,411
  • 28
  • 180
  • 220
  • Are there any recommendations how to format things when overriding ToString, e.g. on e.g. a DisposersFailedException which may contain multiple exceptions if multiple exceptions were thrown in the course of disposing an object? – supercat Dec 15 '10 at 21:52
3

Exception.ToString() ?

Joe
  • 118,426
  • 28
  • 194
  • 329
1

For printing exceptions in C#, you can use Debug.WriteLine():

    try
    {
        reader = cmd.ExecuteReader();
    }
    catch (Exception ex)
    {
        Debug.WriteLine("<<< catch : "+ ex.ToString());
    }

Also, you can use this for other exceptions, for example with MySqlException:

   try
    {
        reader = cmd.ExecuteReader();
    }
    catch (MySqlException ex)
    {
        Debug.WriteLine("<<< catch : "+ ex.ToString());            
    }

But, if you need printing a Exception's Message, you have to use Message:

    try
    {
        reader = cmd.ExecuteReader();
    }
    catch (MySqlException ex)
    {
        Debug.WriteLine("<<< catch : "+ ex.Message);            
    }
Sergio Perez
  • 543
  • 5
  • 4