gpt4 book ai didi

Kotlin - 接口(interface)方法引发的文档异常

转载 作者:行者123 更新时间:2023-12-02 12:54:54 25 4
gpt4 key购买 nike

由于 Kotlin 没有检查异常,记录接口(interface)方法预期抛出的异常的正确方法是什么?我应该在接口(interface)中还是在实现类中记录它(仅当具体方法实际抛出它时)?

最佳答案

由于客户端程序针对该接口(interface)进行编程,因此我建议在该接口(interface)的 Javadoc/KDoc 中制作文档。 this 中讨论了你是否真的应该记录它们。线程例如:

Oracle建议:

If it's so good to document a method's API, including the exceptions it can throw, why not specify runtime exceptions too?
Runtime exceptions represent problems that are the result of a programming problem, and as such, the API client code cannot reasonably be expected to recover from them or to handle them in any way. Such problems include arithmetic exceptions, such as dividing by zero; pointer exceptions, such as trying to access an object through a null reference; and indexing exceptions, such as attempting to access an array element through an index that is too large or too small.
Runtime exceptions can occur anywhere in a program, and in a typical one they can be very numerous. Having to add runtime exceptions in every method declaration would reduce a program's clarity.



因此,如果信息对客户有用,则应记录在案(即客户是否可以处理,例如 IOException )。对于“常规”运行时异常,例如 IllegalArgumentException我会说“不”,不要记录它们。

关于Kotlin - 接口(interface)方法引发的文档异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47511973/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com