Why is it discouraged to throw a generic (java.lang.Exception) exception, when it is usually sufficient to handle most conditional failures within a method? I understand tha
If you throw more specific exceptions, that does not stop any calling code from dealing with all of them in one Exception
catch block. Being more specific is more documenting as to what type of errors occur, making it easier for programmers to deal with them separately.
Additionally, by throwing "Exception" itself you're basically telling callers of your code that they can't rule out any class of exception. An IOException
might occur, as might a NumberFormatException
, etc.