Java 抛出 RuntimeException 的方法是否应该在方法签名中指出它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/824217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Should methods that throw RuntimeException indicate it in method signature?
提问by Jacques René Mesrine
For example, many methods in frameworks/JDK might throw
例如,框架/JDK 中的许多方法可能会抛出
java.lang.SecurityException
but this is not indicated in the method signature (since that is a practice normally reserved for checked exceptions). I want to argue that declaring RuntimeExceptions in method sigs has many benefits (akin to static type checking for example). Am I drunk or otherwise?
但这并没有在方法签名中指明(因为这是通常为检查异常保留的做法)。我想论证在方法 sigs 中声明 RuntimeExceptions 有很多好处(例如类似于静态类型检查)。我是喝醉了还是其他?
采纳答案by Robin
I would not declare an unchecked exception in the signature, since it is misleading to the user of that API. It is no longer obvious whether the exception has to be explicitly handled.
我不会在签名中声明未经检查的异常,因为它会误导该 API 的用户。是否必须显式处理异常不再明显。
Declaring it in the javadoc is a better approach since it allows someone to handle it if they think it is necessary, but knowing they can ignore it if they want. This makes the separation between checked and unchecked clear.
在 javadoc 中声明它是一种更好的方法,因为它允许某人在他们认为必要时处理它,但知道他们可以根据需要忽略它。这使得已检查和未检查之间的区别变得清晰。
回答by Patrick Cornelissen
In my point of view it's better to declare runtime exceptions at least in the javadoc for the method. Declaring it in the signature makes it even more obvious what may happen when something goes wrong. This is my main reason for suggesting to provide this information.
在我看来,最好至少在该方法的 javadoc 中声明运行时异常。在签名中声明它可以让出现问题时可能发生的事情变得更加明显。这是我建议提供此信息的主要原因。
FYI: as time has progressed (now in 2017) I am leaning now far more to documenting them in javadoc only and avoiding checked exceptions as much as possible.
仅供参考:随着时间的推移(现在是 2017 年),我现在更倾向于仅在 javadoc 中记录它们并尽可能避免检查异常。
回答by kgiannakakis
This has to do with the discussion regarding checked exceptions. Most would agree that exceptions shouldn't be declared in methods signatures.
这与有关受检异常的讨论有关。大多数人都同意不应在方法签名中声明异常。
There is also a discussionregarding how runtime exceptions should be used. I agree with one poster that runtime exceptions should denote a programming error or a fatal condition. So there isn't much merit declaring them in the signature. Every method could potentially through one.
还有一个关于如何使用运行时异常的讨论。我同意一张海报,即运行时异常应表示编程错误或致命情况。因此,在签名中声明它们没有多大意义。每种方法都可能通过一种方法。
回答by Kris
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.)
但是,如果某个方法可能会抛出一些未经检查的异常,请注意 Javadoc 中 @throws 中的可能情况,这有助于其他调用该方法的人了解可能出错的地方。这仅对调用者可能能够处理的异常有用(例如由于输入错误等导致的 NPE)
回答by Sam Barnum
Take a look at the javadoc for Collection#add
查看 Collection#add 的 javadoc
There's a whole slew of unchecked exceptions mentioned:
提到了一大堆未经检查的异常:
Throws:
UnsupportedOperationException - add is not supported by this collection.
ClassCastException - class of the specified element prevents it from being added to this collection.
NullPointerException - if the specified element is null and this collection does not support null elements.
IllegalArgumentException - some aspect of this element prevents it from being added to this collection.
If you have the patience, I'd recommend thoroughly documenting the possible exceptions thrown by your methods this way. In a way, it's even more important to do this for unchecked exceptions, as checked exceptions are somewhat self-documenting (the compiler forces the calling code to acknowledge them).
如果您有耐心,我建议您以这种方式彻底记录您的方法可能引发的异常。在某种程度上,对未检查异常执行此操作更为重要,因为已检查异常在某种程度上是自记录的(编译器强制调用代码确认它们)。
回答by alphazero
If you are writing an api for use by others, then there is ample reason for explicit documentation of your intent in the api and there is no downside to declaring RuntimeExceptions in the method signature.
如果您正在编写供其他人使用的 api,那么有充分的理由在 api 中明确记录您的意图,并且在方法签名中声明 RuntimeExceptions 没有任何缺点。
回答by Dheeraj Vepakomma
From the Oracle Java tutorial:
"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.
“如果记录方法的 API 非常好,包括它可以抛出的异常,为什么不也指定运行时异常呢?” 运行时异常表示由编程问题导致的问题,因此,不能合理地期望 API 客户端代码从它们中恢复或以任何方式处理它们。此类问题包括算术异常,例如除以零;指针异常,例如尝试通过空引用访问对象;和索引异常,例如尝试通过太大或太小的索引访问数组元素。
运行时异常可能发生在程序中的任何地方,而且在一个典型的异常中,它们可能非常多。必须在每个方法声明中添加运行时异常会降低程序的清晰度。