Java gets the exception name only if there is no stacktrace

How do I get the exception name without getting the stack trace?

I'm using exception Tostring() converts the thrown exception to a string, but I only want the exception name likenullpointerexception, not the entire stack trace

How can I solve it?

Solution

exception.getClass().getSimpleName();
exception.getClass().getSimpleName();

Class#getSimpleName()

Note: if your exception is an anonymous class (although I personally have never seen an anonymous exception in any production code), this will not work properly

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>