我可以在构造函数中使用throws吗? [英] Can I use throws in constructor?
本文介绍了我可以在构造函数中使用throws吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须在构造函数中初始化文件对象并处理异常,使用 throws
是否有效,或者我应该去尝试
/ catch
?
I have to initialize file objects inside the constructor and for handling the exception, is it efficient using throws
or should I go for try
/catch
?
推荐答案
可以投掷构造函数中的异常。我知道一些Java库类这样做( URI 只是一个例子)。我认为抛出异常比返回未知或无效状态的对象更好。
It's okay to throw an exception in the constructor. I know some of the Java library classes do so (URI for just one example). I think it's better to throw an exception than to return an object in an unknown or invalid state.
这篇关于我可以在构造函数中使用throws吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文