Java:初始化错误的适当异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9684500/
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
Java: Appropriate exception for initialization error
提问by Adam Matan
Which exception should I throw when a static factory method fails to initialize a new object? I prefer raising a meaningful exception rather than returning null
.
当静态工厂方法无法初始化新对象时,我应该抛出哪个异常?我更喜欢提出一个有意义的异常而不是返回null
。
采纳答案by Edwin Buck
If you are throwing an exception in a Factory due to insufficient data, I like to throw an IllegalStateException
with a description similar to "cannot construct X, no Y has been set".
如果由于数据不足而在工厂中抛出异常,我喜欢抛出IllegalStateException
类似“无法构造 X,未设置 Y”的描述。
If you are throwing an exception in a Factory due to conflicting data, I like to throw an
IllegalStateException
with a description similar to "cannot construct X, Y conflicts with Z".
如果由于数据冲突而在工厂中抛出异常,我喜欢抛出
IllegalStateException
类似“无法构造 X,Y 与 Z 冲突”的描述。
If you are throwing an exception in a Factory due to a bad (or nonsensical) value, I like to throw an IllegalArgumentException
with a description similar to "Y cannot be A".
如果由于错误(或无意义的)值而在工厂中抛出异常,我喜欢抛出IllegalArgumentException
类似“Y 不能是 A”的描述。
If you are throwing an exception in a Factory due to a missing value, I like to throw an IllegalArgumentException
with a description similar to "Y cannot be null".
如果由于缺少值而在工厂中抛出异常,我喜欢抛出一个IllegalArgumentException
类似“Y 不能为空”的描述。
The last preference is up to some debate. Some people suggest that it might be better to throw a NullPointerException
; in my case, we avoid them at all costs since many customers tend to not read the exception message (and assume that NullPointerException means a coding error).
最后一个偏好取决于一些辩论。有些人建议扔一个NullPointerException
; 就我而言,我们不惜一切代价避免它们,因为许多客户往往不阅读异常消息(并假设 NullPointerException 意味着编码错误)。
In any event, you should provide a good, specific, message as to why the exception was thrown, to ease your future support costs of seeing that exception raised a few months from now.
在任何情况下,您都应该提供关于抛出异常的原因的良好、具体的消息,以减轻您在几个月后看到该异常引发的未来支持成本。
回答by Rahul Borkar
You can create your own Exception by extending Exception class
您可以通过扩展 Exception 类来创建自己的 Exception
回答by Oscar Gomez
Something like this probably should just be an Assert, but if there is in fact the possibility for this to fail then a custom exception that is meaningful to you would be my choice.
像这样的事情可能应该只是一个断言,但如果实际上有可能失败,那么对您有意义的自定义异常将是我的选择。
回答by Nishant
Ideally you would like to extend Exception
and craft your own IntializatonException
.
理想情况下,您希望扩展Exception
和制作自己的IntializatonException
.
回答by Jakub Zaverka
Yeah, the cause of the problem is your best bet. If the arguments are not OK, you can throw IllegalArgumentException, if some file is not there, you can throw FileNotFoundException, if the factory is not initialized properly, you can throw IllegalStateException, etc., etc...
是的,问题的原因是你最好的选择。如果参数不正确,你可以抛出IllegalArgumentException,如果某个文件不存在,你可以抛出FileNotFoundException,如果工厂没有正确初始化,你可以抛出IllegalStateException,等等......
However, creating your own Exception is easy. Just declare you class as extends Exception
and add delegate constructors. If you extend Exception, then that method which can throw it must be declared with throws
. If you don't want that, you can extend RuntimeException, those need not be declared.
但是,创建自己的异常很容易。只需将您的类声明为extends Exception
并添加委托构造函数。如果扩展 Exception,那么可以抛出它的方法必须用throws
. 如果您不希望那样,您可以扩展 RuntimeException,这些不需要声明。