25

Is this the best method for getting the name of a specific Exception in C#:

ex.GetType().ToString()

It is in a generic exception handler:

catch (Exception ex)
Lifu Huang
  • 11,930
  • 14
  • 55
  • 77
Robben_Ford_Fan_boy
  • 8,494
  • 11
  • 64
  • 85

2 Answers2

40

ex.GetType().Name or ex.GetType().FullName for the fully qualified name.

Lifu Huang
  • 11,930
  • 14
  • 55
  • 77
15

Try ex.GetType().Name

try
{           
    object test = null;
    test.ToString();
}
catch (Exception ex)
{   
    Console.WriteLine(ex.GetType().Name);
}

Gives this..

NullReferenceException
Sateesh Pagolu
  • 9,282
  • 2
  • 30
  • 48