how to handle cases that should have many exceptions?

kmilo93sd

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

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Where and how should I handle multiple exceptions?

From Dev

How should I handle filling multiple textboxes when I don't know how many of them will have data?

From Dev

how can i handle value error exceptions many times in python

From Dev

How many NSOperationQueues should an app have?

From Dev

How many confirmations should I have on ethereum?

From Java

How should you handle MongoDB-Exceptions in Java?

From Java

How should I handle exceptions when using SwingWorker?

From Java

If the catch throws exceptions, how should I handle them?

From Dev

How should I handle exceptions in custom Hamcrest matchers?

From Dev

how to handle unchecked exceptions?

From Dev

How to handle exceptions with builders

From Dev

How Exceptions are handle in EJB?

From Dev

How to handle FirebaseAuth exceptions

From Dev

How to handle exceptions in games

From Dev

How to handle exceptions in Frege?

From Dev

How to handle exceptions in Kotlin?

From Dev

Deno: How to handle exceptions

From Dev

How many View Controllers should I have in my game?

From Dev

DDD: How many aggregates should have a single bounded context?

From Dev

How many dead letter queues should I have on AWS

From Dev

LSTM, Keras : How many layers should the inference model have?

From Dev

How many model classes should I have entity?

From Dev

How many entities a database should have in a real world app?

From Dev

What is /dev/ram* and how many should I have on my HDD?

From Dev

How to handle UIWebView cases were no connection, and should I use Network indicator?

From Dev

How to handle mixed cases regex?

From Dev

How to handle redundant cases in regex?

From Dev

IQueryable, how to handle particular cases

From Dev

Is there a more innovative way to handle null exceptions in the following cases?

Related Related

  1. 1

    Where and how should I handle multiple exceptions?

  2. 2

    How should I handle filling multiple textboxes when I don't know how many of them will have data?

  3. 3

    how can i handle value error exceptions many times in python

  4. 4

    How many NSOperationQueues should an app have?

  5. 5

    How many confirmations should I have on ethereum?

  6. 6

    How should you handle MongoDB-Exceptions in Java?

  7. 7

    How should I handle exceptions when using SwingWorker?

  8. 8

    If the catch throws exceptions, how should I handle them?

  9. 9

    How should I handle exceptions in custom Hamcrest matchers?

  10. 10

    how to handle unchecked exceptions?

  11. 11

    How to handle exceptions with builders

  12. 12

    How Exceptions are handle in EJB?

  13. 13

    How to handle FirebaseAuth exceptions

  14. 14

    How to handle exceptions in games

  15. 15

    How to handle exceptions in Frege?

  16. 16

    How to handle exceptions in Kotlin?

  17. 17

    Deno: How to handle exceptions

  18. 18

    How many View Controllers should I have in my game?

  19. 19

    DDD: How many aggregates should have a single bounded context?

  20. 20

    How many dead letter queues should I have on AWS

  21. 21

    LSTM, Keras : How many layers should the inference model have?

  22. 22

    How many model classes should I have entity?

  23. 23

    How many entities a database should have in a real world app?

  24. 24

    What is /dev/ram* and how many should I have on my HDD?

  25. 25

    How to handle UIWebView cases were no connection, and should I use Network indicator?

  26. 26

    How to handle mixed cases regex?

  27. 27

    How to handle redundant cases in regex?

  28. 28

    IQueryable, how to handle particular cases

  29. 29

    Is there a more innovative way to handle null exceptions in the following cases?

HotTag

Archive