0

This is my scenario, I have a list of exceptions of contains arbitrary exceptions like from different hierarchy, and the bellow code snaps will explain what I need to do

private List<java.lang.Class> connectionExceptions;
try {

// trying to connect to external module;

} catch(Exception e) {
   // Need to compare this exception e with a list of exceptions which I have, 
   // and the action depends on the results. There may be some other exception 
   // which are not in the list.
}

So, how can I compare the exceptions, I need to some sort of actions if the exception is in the list or some other...

I know, control flow depends on the exception is not good practice. But, I need to do it.

Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105

5 Answers5

3

You can have several catch-blocks each catching only a specific exception.

...
catch(SomeException e) {
  //do something
}
catch(SomeOtherException e) {
  //do something else
}

Or, to really check if the exception is in your list, use connectionExceptions.contains(e.getClass()).

tobiasbayer
  • 10,269
  • 4
  • 46
  • 64
2

Since Exception class uses equals() function of Object class, connectionExceptions.contains(e) will not yield a correct answer as each Exception e instance bearing the same cause and stack trace will have different default HEX identity. However if you do want to compare two exceptions, Arrays.deepEquals(Object, Object) can be used.

catch (Exception e) {
  boolean sameException = false;
  for (Exception ee : connectionExceptions) {
    sameException = Arrays.deepEquals(e.getStackTrace(), ee.getStackTrace());
    if (sameException) {
      break;  //exception matched
    }
  }
}
KrishPrabakar
  • 2,824
  • 2
  • 31
  • 44
  • Please provide some explanations why `Arrays.deepEquals()` should be preferred over `Arrays.equals()` – coolguy Jun 21 '16 at 10:00
1
if (connectionExceptions.contains(e) {
   //
}

That's if you need the list to be dynamic. Otherwise use:

} catch (FooException) {

} catch (BarException) {

}

Or the multi-catch in Java7:

} catch (FirstException | SecondException ex) {
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • oops, yes. Fixing. I would expect it to be `Class` actually, it makes more sense – Bozho Dec 22 '11 at 09:24
  • I guess `Exception` doesn't have a proper overridden `equals()` method, causing it to use the super class `Object`'s equals which uses identity comparison (`==`). – KrishPrabakar Mar 09 '15 at 10:59
0

Peter Knego explains how to compare exceptions in this answer.

Basically, you can use instanceof to compare.

for (java.lang.Class exception : connectionExceptions) {
    if (e instanceof exception) {
        // DO YOUR THING
        break;
    }
}
Community
  • 1
  • 1
Bugs Happen
  • 2,169
  • 4
  • 33
  • 59
-1

Abimaran, your question is "check whether the exception (thrown in try block ) is in your list or not". So in order to check this, checking mechanism should be very straight forward or detailed, i mean,

  • Maintain the list, Map or any thing of each Exception in detail (like if poss bile, type ,class etc of the exception)
  • When you check the exception in catch block, just use some of the functions which is provided by the Java generic classes.

That is the best you can do for this scenario.

Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105