32

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
  • 10,940
  • 26
  • 72
  • 108
  • 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

57
Console.WriteLine(exception.ToString());
jgauffin
  • 99,844
  • 45
  • 235
  • 372
7

the ToString method of Exception does exactly that.

Klaus Byskov Pedersen
  • 117,245
  • 29
  • 183
  • 222
  • 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
  • 122,218
  • 32
  • 205
  • 338
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
  • 571
  • 6
  • 6