23

I have a statement that throws a lot of checked exceptions. I can add all catch blocks for all of them like this:

try {
    methodThrowingALotOfDifferentExceptions();
} catch(IOException ex) {
    throw new MyCustomInitializationException("Class Resolver could not be initialized.", ex);
} catch(ClassCastException ex) {
    throw new MyCustomInitializationException("Class Resolver could not be initialized.", ex);
} catch...

I do not like this because they are all handled same way so there is kind of code duplication and also there is a lot of code to write. Instead could catch Exception:

try {
    methodThrowingALotOfDifferentExceptions();
} catch(Exception ex) {
    throw new MyCustomInitializationException("Class Resolver could not be initialized.", ex);
}

That would be ok, except I want all runtime exceptions to be thrown away without being caught. Is there any solution to this? I was thinking that some clever generic declaration of the type of exception to be caught might do the trick (or maybe not).

Rasto
  • 17,204
  • 47
  • 154
  • 245

3 Answers3

53

You could do the following:

try {
    methodThrowingALotOfDifferentExceptions();
} catch(RuntimeException ex) {
    throw ex;
} catch(Exception ex) {
    throw new MyCustomInitializationException("Class Resolver could not be initialized.", ex);
}
bdoughan
  • 147,609
  • 23
  • 300
  • 400
15

If you can use Java 7, you can use a Multi-Catch:

try {
  methodThrowingALotOfDifferentExceptions();
} catch(IOException|ClassCastException|... ex) {
  throw new MyCustomInitializationException("Class Resolver could not be initialized.", ex);
}
Urs Reupke
  • 6,791
  • 3
  • 35
  • 49
  • +1 alerting me to this new valuable (and long overdue IMO) feature in Java 7 – peter.murray.rust Nov 14 '12 at 12:34
  • +1 For this one reason I'd use Java 7 if I could. But I cannot and I'm very sorry for that because this Multi-Catch is perfect. I have to use Java 6 :((( – Rasto Nov 14 '12 at 12:38
2

You could try something like this, to basically catch everything and then re-throw the RuntimeException if it is an instance of that class...

try {
    methodThrowingALotOfDifferentExceptions();
} catch(Exception ex) {
    if (ex instanceof RuntimeException){
        throw ex;
    }
    else {
        throw new MyCustomInitializationException("Class Resolver could not be initialized.", ex);
    }
}

Seeing as though this would be messy to write over and over again (and bad for maintainability), I'd probably move the code into a different class, something like this...

public class CheckException {
    public static void check(Exception ex, String message) throws Exception{
        if (ex instanceof RuntimeException){
            throw ex;
        }
        else {
            throw new MyCustomInitializationException(message, ex);
        }
    }
}

And use it in your code like this...

try {
    methodThrowingALotOfDifferentExceptions();
} catch(Exception ex) {
    CheckException.check(ex,"Class Resolver could not be initialized.");
}

Noting that we pass in the message so that we can still customise our MyCustomInitializationException.

wattostudios
  • 8,666
  • 13
  • 43
  • 57
  • The last option would be good if I needed to do it several times but that's not my case - I only need to use it once. Only I'd parametrize the static method with `String` message. – Rasto Nov 14 '12 at 14:07
  • you need to cast `ex` to `RuntimeException` in the code. –  Dec 08 '16 at 15:52