1

I had to use reflection to instantiate a group of classes. With that all good, but reflection uses many exceptions and my method looks ugly.

What do good practices advise in these cases?

It is supposed to throw these exceptions and catch them in a high level class to be able to give clear information about the error, but if I'm going through 6 or 8 exceptions among all the methods and classes involved, the code will be horrible, chaotic and very horrible.

private Filter getFilterInstance(String path){
    try {
        return (Filter) Class.forName(path).getConstructor().newInstance();
    } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
        Logger.getLogger(FiltersBuilder.class.getName()).log(Level.SEVERE, null, ex);
        return null;
    }
}
2
  • You can throw always catch/throw Exception... Commented Jul 4, 2018 at 3:09
  • 1
    @shmosel is right. You can just throw Exception and catch Exception in the main calling method or catch exception in every method and throw a runtime exception which you do not need to catch anywhere. Commented Jul 4, 2018 at 3:13

4 Answers 4

1

You can catch ReflectiveOperationException

It is the super type of:

  • ClassNotFoundException
  • IllegalAccessException
  • InstantiationException
  • InvocationTargetException
  • NoSuchFieldException
  • NoSuchMethodException

Which means you just need to:

} catch (ReflectiveOperationException | SecurityException | IllegalArgumentException ex) {
    Logger.getLogger(FiltersBuilder.class.getName()).log(Level.SEVERE, null, ex);
    return null;
}

Since SecurityException & IllegalArgumentException are actually Runtime Exceptions, you could:

} catch (ReflectiveOperationException ex) {
    Logger.getLogger(FiltersBuilder.class.getName()).log(Level.SEVERE, null, ex);
    return null;
}

if you are OK with the RuntimeException propagating to callers.

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

Comments

0

Best way is to handle them seperately.
Reason

..
catch(Exception1 | Exception2 ex)
  1. It will become an overhead to check which exception occured and then writing ways to handle it.

    Why do you want to add unnecessary if statements to check which exception occured?

  2. Simply, your code becomes unreadable.

Comments

0

Many people claim that the war between checked and unchecked exceptions is over.

Unchecked won!

So the simple answer is: surround your code with a simple catch for Exception on the lowest level, put that exception into a (maybe specialized) RuntimeException and rethrow that.

Then, on that higher layer that needs it, catch the wrapping exception, access its cause and deal with it appropriately. Where: most likely, almost all of the different checked exceptions you are looking at here resemble an internal error. So there isn't much of "different error message" in the first place.

Comments

0

If each exception type requires different handling logic, use separate catch blocks.

If several of them require the same handling logic, catch those ones in the same block.

If all possible exceptions require the same handling logic, catch the base Exception type. This looks like the correct solution for your code because you are passing them into your logger regardless of which type they are.

private Filter getFilterInstance(String path){
    try {
        return (Filter) Class.forName(path).getConstructor().newInstance();
    } catch (Exception ex) {
        Logger.getLogger(FiltersBuilder.class.getName()).log(Level.SEVERE, null, ex);
        return null;
    }
}

Comments

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.