Java only get Exception name without StackTrace

25,674
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)

Share:
25,674
Broadwell
Author by

Broadwell

Updated on September 13, 2020

Comments

  • Broadwell
    Broadwell over 3 years

    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?