Should methods that throw RuntimeException indicate it in method signature?

后端 未结 7 1712
小鲜肉
小鲜肉 2020-11-30 23:43

For example, many methods in frameworks/JDK might throw

java.lang.SecurityException 

but this is not indicated in the method signature (si

7条回答
  •  生来不讨喜
    2020-12-01 00:06

    In my view unchecked exceptions should never be declared in the method signature as that is contrary to their nature.

    If, however, a method is likely to throw some unchecked exceptions noting the likely circumstances in @throws in Javadoc can be helpful for others invoking the method in understanding what can go wrong. This is only useful though for exceptions that the callers is likely to be able to handle (such as a NPE due to bad input etc.)

提交回复
热议问题