Robben_Ford_Fan_boy
Robben_Ford_Fan_boy

Reputation: 8738

Get name of specific Exception

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)

Upvotes: 30

Views: 19043

Answers (2)

Sateesh Pagolu
Sateesh Pagolu

Reputation: 9606

Try ex.GetType().Name

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

Gives this..

NullReferenceException

Upvotes: 16

Lifu Huang
Lifu Huang

Reputation: 12899

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

Upvotes: 48

Related Questions