Should I declare all exceptions thrown from a method in the method signature or just the super class of the exceptions?

前端 未结 3 907
悲哀的现实
悲哀的现实 2021-01-14 16:08

When I throw checked exceptions from a method should I just declare the super class of the exceptions in the method signature or all the different types? If I have the follo

3条回答
  •  说谎
    说谎 (楼主)
    2021-01-14 16:26

    The throws clause is there to convey useful information to the calling method about what might go wrong during invocation of this method. That means that how specific you are will depend on how much information you want to convey; and that will be application-dependent.

    For instance, declaring throws Exception is almost always a bad idea: the information this conveys is just "something might go wrong", which is too vague to be useful. But whether calling classes are going to need perfectly fine-grained information in the throws clause is something you need to decide by looking at your program. There's no set answer.

提交回复
热议问题