Java – catch several exceptions and re throw the general exception
I'm using reflection to add some data to private variables in classes from third-party libraries There are about four different exceptions to throw along the way; All this is about reflection, and all this is unlikely to happen I'm hard coding the names of the classes and variables involved I'm unlikely to receive any missing class or missing field errors unless the library is upgraded one day and has changed significantly
I'd rather not declare all four exceptions for my caller to handle He may never see them I want to catch all this and throw another exception, saying "a java reflection error has occurred; it may be that the library has been upgraded and changed in a way incompatible with this method." Is there a standard Java exception I can throw that represents a general reflection error? Should I define myself? Or is it better to just declare that this method can throw all possible reflection exceptions?
Solution
If you never expect them to happen, you can convert all exceptions to assertionerror
} catch (InvocationTargetException e) { // Throw any exception in the current thread (even if it is a checked exception) Thread.currentThread().stop(e.getCause()); }