简体   繁体   中英

Is it possible to catch all exceptions except runtime exceptions?

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

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);
}

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);
}

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM