37

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 likeNullPointerException and not the entire stack trace.

How can I solve this?

Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
Broadwell
  • 1,343
  • 2
  • 19
  • 33

1 Answers1

62
exception.getClass().getSimpleName();

Class#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)

S. Pauk
  • 5,208
  • 4
  • 31
  • 41