[英]Java only get Exception name without StackTrace
how can I get the exception name without getting the stack trace? 如何在不获取堆栈跟踪的情况下获取异常名称?
I am using exception.toString()
to convert the thrown exception into a string, but I only want the exception name like NullPointerException
and not the entire stack trace. 我使用
exception.toString()
将抛出的异常转换为字符串,但我只想要像NullPointerException
这样的异常名称而不是整个堆栈跟踪。
How can I solve this? 我怎么解决这个问题?
exception.getClass().getSimpleName();
Class#getSimpleName() 类#getSimpleName()
NOTE: this will not work in case if your exception is an anonymous class (although I have personally never seen an anonymous exception in any production code) 注意:如果你的异常是一个匿名类,这将不起作用(虽然我个人从未在任何生产代码中看到过匿名异常)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.