Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get name of specific Exception

Tags:

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) 
like image 259
Robben_Ford_Fan_boy Avatar asked Dec 23 '16 00:12

Robben_Ford_Fan_boy


People also ask

How do I get the exception name?

Using printStackTrace() method − It print the name of the exception, description and complete stack trace including the line where exception occurred. Using toString() method − It prints the name and description of the exception. Using getMessage() method − Mostly used. It prints the description of the exception.

How do you catch a specific exception in Python?

Catching Exceptions in Python In Python, exceptions can be handled using a try statement. The critical operation which can raise an exception is placed inside the try clause. The code that handles the exceptions is written in the except clause.

How do you delete an exception in Python?

The sys. exc_clear() statement can be utilized to clear the last thrown exception of the Python interpreter. The following code uses the sys. exc_clear() statement in the except block to ignore an exception and proceed with the code in Python.


2 Answers

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

like image 159
Lifu Huang Avatar answered Oct 19 '22 20:10

Lifu Huang


Try ex.GetType().Name

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

Gives this..

NullReferenceException 
like image 42
Sateesh Pagolu Avatar answered Oct 19 '22 21:10

Sateesh Pagolu