How to get Type of Exception in C#
To get exception type in C# we can directly useGetType()
method that returns a Type object which also has ToString()
method. Example of getting exception type in C#
In below example we are forcing the application to cause aFormatException
by tryin to convert a string to Int. In the catch section you will the code how to get Exception Type.using System;
public class Program
{
public static void Main()
{
try {
/* Below code is used to cause a FormatException */
string a="asd";
int s = Convert.ToInt32(a);
} catch(Exception ex) {
Console.WriteLine(ex.GetType().ToString());
}
}
}
Output
System.FormatException