38

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?

0

1 Answer 1

63
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)

Sign up to request clarification or add additional context in comments.

2 Comments

won't work with some jar libraries too. will throw class not found exception.
@Eboubaker Can you provide some example?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.