Javadoc and RuntimeException

眉间皱痕 提交于 2020-01-03 00:32:30

问题


I am curious about if I should add throws ExceptionClass or not after the method's signature.(ExceptionClass extends RuntimeException)

For instance:

public void foo() // throws ExceptionClass
{
    // ...
    throw new ExceptionClass("");
}

回答1:


Ideally you don't need to add runtime exception in method's throws clause. Since you want the consumer of this method to be aware of chances that this method may throw exception , i would say use javadoc either. Below is example of how you should use :

/**
 * 
 * @throws ExceptionClass
 */
public void foo()
{
    // ...
    throw new ExceptionClass("");
}



回答2:


If you use the JDK as a guidance, RuntimeException are not in the methods signature but are documented in the javadoc. You can have a look at collections for example.




回答3:


Runtime exceptions do not need to be listed in the throws clause. Place it there only if you need to document this behaviour, if it's in some way important to know for the client of this class, that this exception might appear (but if that's the case, perhaps it needs to be changed to a checked exception, or only be mentioned in the javadocs).



来源:https://stackoverflow.com/questions/16757066/javadoc-and-runtimeexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!