2017  Kodetalk | Feedback | Privacy Policy | Terms | About
userimage

How to check if a specific exception exist in the stacktrace from the thrown Exception

i have a situation where i need to find out if a specific exception exist in the thrown stacktrace exception. is there any way to do so?

userimage

I have not tested the code but this can give you how to get the specific exception from a stacktrace:


  1. public static E findSpecificException(Throwable stackTrace, SpecificexceptionClass ex) {
  2. if (t instanceof ex) {
  3. return (ex) t;
  4. }
  5. if (t.getCause() != null) {
  6. return findSpecificException(t.getCause(), ex);
  7. }
  8. return null;
  9. }
Answer is