The only Java mechanism is to choose which exceptions for catch are specific exception classes. If you want to distinguish the exceptions of the same class depending on their causes, then you should catch all the exceptions of this class, as you demonstrate.
Note, however, that it is problematic to throw the exception as soon as you catch it, because it replaces the original stack trace with a new, context-specific new throw . This can make debugging a lot more difficult. To avoid this, you will need to wrap the exception of the individual new exception caught as the reason and throw it away.
source share