java 我应该在函数中抛出 IllegalArgumentException 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5304098/
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 I put throws IllegalArgumentException at the function?
提问by Ismail Marmoush
I'm building a scientific software with lots of calculations and of course arguments can have wrong lengths etc... So I used IllegalArgumentException
class as it seemed right name for the issue, but should I put the throws IllegalArgumentException
at the function definition ?
我正在构建一个包含大量计算的科学软件,当然参数可能有错误的长度等等......所以我使用了IllegalArgumentException
class ,因为它似乎是这个问题的正确名称,但我应该把它throws IllegalArgumentException
放在函数定义中吗?
I am asking this because after I wrote it, the Eclipse Editor didn't ask me to surround the function with try and catch. I thought this is how try and catch were enforced. I've read the Exception handling tutorial at Java.com yet I'm not sure I understood the part regarding my question right though.
我问这个是因为在我写完之后,Eclipse 编辑器没有要求我用 try 和 catch 包围这个函数。我认为这就是 try 和 catch 的执行方式。我已经阅读了 Java.com 上的异常处理教程,但我不确定我是否理解关于我的问题的部分。
回答by ILMTitan
RuntimeException
s like IllegalArgumentException
are used to indicate programming errors. The program itself should rarely be able to handle it. Someone needs to manually fix the code.
RuntimeException
s likeIllegalArgumentException
用于指示编程错误。程序本身应该很少能够处理它。有人需要手动修复代码。
Potential RuntimeException
s should be documented somehow in the function contract (i.e. javadoc), either with the explicit @throws
, or while describing the inputs. If you don't have a javadoc for the function, you may want to add the throws clause just to document the potential pitfalls of using the function, but in general adding throws clauses for runtime exceptions is discouraged.
PotentialRuntimeException
应该以某种方式记录在函数契约(即 javadoc)中,或者使用显式@throws
,或者在描述输入时。如果您没有该函数的 javadoc,您可能希望添加 throws 子句以记录使用该函数的潜在缺陷,但通常不鼓励为运行时异常添加 throws 子句。
If giving a wrong length is not actually a programming error, but is an exception situation, I would create a new checked exception (such as BadLengthError). If it is not an exceptional situation, don't use exceptions for flow control.
如果给出错误的长度实际上不是编程错误,而是异常情况,我将创建一个新的检查异常(例如 BadLengthError)。如果不是异常情况,不要使用异常进行流量控制。
回答by corsiKa
There's two types of exceptions:
有两种类型的异常:
runtime exceptions (like IllegalArgumentException and NullPointerException for example) don't need to be explicitly caught, because they 'shouldn't happen'. When they do, of course, you need to handle them somewhere.
运行时异常(例如 IllegalArgumentException 和 NullPointerException)不需要显式捕获,因为它们“不应该发生”。当然,当它们发生时,您需要在某个地方处理它们。
regular exceptions NEED to be caught or declared to be thrown because they represent a more intrinsically difficult kind of error.
常规异常需要被捕获或声明为抛出,因为它们代表了一种本质上更困难的错误。
回答by colinjwebb
You need to read up on Unchecked Exceptions - exceptions which inherit from RuntimeException. They don't need to be declared in the method header.
您需要阅读 Unchecked Exceptions - 从 RuntimeException 继承的异常。它们不需要在方法头中声明。
http://download.oracle.com/javase/tutorial/essential/exceptions/runtime.html
http://download.oracle.com/javase/tutorial/essential/exceptions/runtime.html
The last paragraph sums it up:
最后一段总结如下:
If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception.
如果可以合理地期望客户端从异常中恢复,请将其设置为已检查异常。如果客户端无法从异常中恢复,请将其设为未经检查的异常。
回答by Jon Bright
IllegalArgumentException
(along with some others, for example NullPointerException
) are examples of a RuntimeException
. This type of exception is not what's known as a checked exception. Java requires that methods declare which checked exceptions they throw and that if a called method might throw a checked exception, the calling method should either declare that it throws the exception itself, or catch and handle it.
IllegalArgumentException
(以及其他一些,例如NullPointerException
)是 a 的示例RuntimeException
。这种类型的异常不是所谓的受检异常。Java 要求方法声明它们抛出哪些已检查异常,如果被调用的方法可能抛出已检查异常,则调用方法应该声明它自己抛出异常,或者捕获并处理它。
As such, my concrete recommendation would be no, don't declare it. Certainly, though, you don't want to be catching it. In most cases, you also don't want to be throwing it unlessthis is unexpectedbehaviour. If it's quite normal and reasonable for the the method to be getting a value it doesn't like, an Exception is the wrong way to handle it.
因此,我的具体建议是不,不要申报。当然,虽然,你不想被抓住。在大多数情况下,除非这是意外行为,否则您也不希望抛出它。如果方法获得它不喜欢的值是很正常和合理的,则异常是错误的处理方式。
You might also want to consider making use of assertions.
您可能还想考虑使用断言。
回答by axcdnt
the first point when understanding exceptions is that they are for exceptional situations. While thinking about your method you must ask: "Should this method throws an exception if an exceptional value is passed through?" If the answer is "yes", put it in your method declaration. I don't know if you get the idea, but it's kind of simple. It's just a matter of practice.
理解异常的第一点是它们是针对异常情况的。在考虑您的方法时,您必须问:“如果传递了异常值,此方法是否应该抛出异常?” 如果答案是“是”,请将其放入您的方法声明中。我不知道你是否明白这个想法,但这有点简单。这只是一个练习问题。