Is it always good to throw exceptions from Utility methods to the caller instead of catching them using try | catch blocks? I know it debatable, but just wanted to get input from different views!
I am not sure what you mean by utility methods, but the rules (or rather, guidelines) apply the same to almost all cases.
General rule of thumb (and I nearly quote it from Effective Java 2nd edition):
Each higher level should catch exceptions from lower levels (and perhaps envelop and re-throw) and throw exceptions that can be understood at a higher level. That means (among other things) - the layer you are throwing the exception from should be sure that its thrown exception can be interpreted at a higher level and taken care of.
I like to think about it that way - if you throw an exception from a method you should be sure that:
There is much much more, but then again - the book I linked to should be a great place to read up on (Java in general too).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With