java InvalidParameterException 或 IllegalArgumentException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4655536/
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
InvalidParameterException or IllegalArgumentException
提问by Cornelius
In Java when should you throw IllegalArgumentException
and when InvalidParameterException
?
Coming from a C# background we would have an ArgumentNullException
that derived from ArgumentException
. If I would want to implement a equivalent ArgumentNullException
/ParameterNullException
in Java should I extend IllegalArgumentException
or InvalidParameterException
?
在 Java 中什么时候应该抛出IllegalArgumentException
,什么时候抛出InvalidParameterException
?来自 C# 背景,我们将有一个ArgumentNullException
派生自ArgumentException
. 如果我想在 Java 中实现等效的ArgumentNullException
/ ParameterNullException
,我应该扩展IllegalArgumentException
还是InvalidParameterException
?
Note: I'm not trying to implement a ArgumentNullException
/ParameterNullException
this would just provide me with a better understanding if I could match up these with the C# framework.
注意:如果我可以将这些与 C# 框架相匹配,我并不是要实现一个ArgumentNullException
/ParameterNullException
这只会让我更好地理解。
采纳答案by Andreas Dolk
There's no apparent need to subclass those exceptions, I'd use them right away to signal, that a method has been called with illegal arguments. I'd always describe the real cause in the exceptions message part.
显然没有必要对这些异常进行子类化,我会立即使用它们来表示已使用非法参数调用了一个方法。我总是在异常消息部分描述真正的原因。
java.security.InvalidParameterException
is already a subclass of IllegalArgumentException
designed for use by the JCA/JCE engine classes(JavaDoc) and I wouldn't use or subclass it in a different context.
java.security.InvalidParameterException
已经是IllegalArgumentException
为 JCA/JCE 引擎类(JavaDoc)使用而设计的子类,我不会在不同的上下文中使用或子类化它。
回答by AlexR
First thanks for your question. Now I know about existence of InvalidParameterException
. This exception belongs to package java.security and according to its javadoc
首先感谢您的提问。现在我知道InvalidParameterException
. 此异常属于包 java.security 并根据其 javadoc
* This exception, designed for use by the JCA/JCE engine classes,
* is thrown when an invalid parameter is passed
* to a method.
IllegalArgumentException belongs to java.lang and therefore can be used for any purpose.
IllegalArgumentException 属于 java.lang,因此可以用于任何目的。
I believe that in 99.9% of cases you should use IllegalArgumentException and use InvalidParameterException only in security context.
我相信在 99.9% 的情况下,您应该只在安全上下文中使用 IllegalArgumentException 并使用 InvalidParameterException。
回答by jpkrohling
There is already an exception to throw when you expect parameters to not be null: NullPointerException. Some programmers thinks that only faulty code throws NullPointerException, but if you really expect the parameters to not be null, and you can't recover from this situation, that's the right exception to throw. From the JavaDoc:
当您期望参数不为空时,已经有一个要抛出的异常:NullPointerException。一些程序员认为只有错误的代码才会抛出NullPointerException,但如果你真的希望参数不为null,并且你无法从这种情况中恢复过来,那才是正确的抛出异常。来自 JavaDoc:
Thrown when an application attempts to use null in a case where an object is required.
当应用程序在需要对象的情况下尝试使用 null 时抛出。
But if we are not talking about null values, but just an invalid value, then you should throw IllegalArgumentException. From the JavaDoc:
但是,如果我们不是在谈论空值,而只是在谈论无效值,那么您应该抛出 IllegalArgumentException。来自 JavaDoc:
Thrown to indicate that a method has been passed an illegal or inappropriate argument.
抛出以指示方法已传递非法或不适当的参数。
As a general suggestion, I would say that you should get familiar with the Java exceptions (from java.lang package), and use them appropriately. If you don't find a suitable exception, then extend one.
作为一般建议,我会说您应该熟悉 Java 异常(来自 java.lang 包),并适当地使用它们。如果找不到合适的异常,则扩展一个。
Also note that the exception "InvalidParameterException" is in the package "java.security", which is probably notwhat you want.
另请注意,异常“InvalidParameterException”位于“java.security”包中,这可能不是您想要的。