Kotlin - 接口方法抛出的文档异常
Kotlin - Document exception thrown by an interface method
由于 Kotlin 没有检查异常,记录接口方法预期抛出的异常的正确方法是什么?我应该在接口中还是在实现中记录它 class (仅当具体方法实际抛出它时)?
由于客户端是针对该接口进行编程的,因此我建议在该接口的 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
)。对于 "regular" 运行时异常,例如 IllegalArgumentException
我会说 "no",不要记录它们。
由于 Kotlin 没有检查异常,记录接口方法预期抛出的异常的正确方法是什么?我应该在接口中还是在实现中记录它 class (仅当具体方法实际抛出它时)?
由于客户端是针对该接口进行编程的,因此我建议在该接口的 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
)。对于 "regular" 运行时异常,例如 IllegalArgumentException
我会说 "no",不要记录它们。