In Java, is using throws Exception instead of throwing multiple specific exceptions good practice?

No, absolutely not. You should specify what exceptions you’re going to throw so the caller can do the right thing with each one. If you don’t, “throws Exception” gets passed up the chain, and the best the callers can do is printStackTrace() and die.

Update: To counter some of the “what if I override the method” objections, I’d go a bit further and say that any time you have a package that throws exceptions (as opposed to passing up an exception from a caller), you should declare an exception class in that package. Thus, if you’re overriding my “addToSchedule() throws ScheduleConflictException”, you’re perfectly able to subclass ScheduleConflictException to do what you need.

Leave a Comment