创建新的 XMLGregorianCalendar 时忽略 DatatypeConfigurationException
当创建这样的新 XMLGregorianCalendar
实例时,我真的需要处理 DatatypeConfigurationException
异常吗?或者我可以安全地抑制它吗?
try {
header.setRequestDateTime(
DatatypeFactory.newInstance().newXMLGregorianCalendar(
new GregorianCalendar()));
} catch (DatatypeConfigurationException e) {
// pass
}
我的解释文档和一些粗略的逻辑说这真的不会除非我给它一些错误的输入,否则会抛出异常。但上例中的情况却并非如此。 JavaDocs 对此是这么说的:
如果由 指定的系统属性DATATYPEFACTORY_PROPERTY, “javax.xml.datatype.DatatypeFactory”存在,名称为 属性的值被实例化。期间抛出的任何异常 实例化过程被包装为 DatatypeConfigurationException。
我是否认为我可以安全地抑制这个已检查的异常?
When creating a new XMLGregorianCalendar
instance like this, do I really need to handle the DatatypeConfigurationException
exception, or can I safely suppress it?
try {
header.setRequestDateTime(
DatatypeFactory.newInstance().newXMLGregorianCalendar(
new GregorianCalendar()));
} catch (DatatypeConfigurationException e) {
// pass
}
My interpretation of the documentation and some rough logic says this wouldn't really throw an exception unless I give it some bad input. And that cannot be the case in the above example. Here's what the JavaDocs say about it:
If the system property specified by DATATYPEFACTORY_PROPERTY,
"javax.xml.datatype.DatatypeFactory", exists, a class with the name of
the property's value is instantiated. Any Exception thrown during the
instantiation process is wrapped as a
DatatypeConfigurationException.
Am I right in thinking that I can safely suppress this checked exception?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
DatatypeConfigurationException
类型的异常可能仅发生在静态方法调用中,因此您只需处理它一次。但您应该处理一次,否则您根本无法创建 XMLGregorianCalendar 实例。
明确地说,调用
never 会抛出
DatatypeConfigurationException
,因此在创建GregorianCalendar
实例的 XML 表示形式时,您不必处理它。 - 从 Java SE API 中,后者仅调用可能会发生 NullPointerException。
The exception of type
DatatypeConfigurationException
may happen only in the static method callTherefore you have to treat it only once. But you should treat it once, otherwise you cannot create your XMLGregorianCalendar instances, at all.
To put it clearly the call
never throws a
DatatypeConfigurationException
, thus you don't have to treat it, when creating XML representations of yourGregorianCalendar
instances. - As from the Java SE API in the latter call only aNullPointerException
can occur.如果您抑制此异常,您将不会设置标头请求时间。从这方面来说,是的,这是一个严重的例外。您无法生成日期和时间实例的 XML 表示形式。
另一方面,您的程序不会崩溃。这不是一个严重虚拟机错误。但你必须处理它。您将无法在运行时修复它。您的服务器和虚拟机环境必须进行相应的设置。
If you opress this exception, you will not set the header request time. In this respect, yes, it is a serious exception. You cannot generate XML representations of your date and time instances.
On the other hand, your program will not crash. It is not a critical VM error. But you have to deal with it. You will not be able to fix it on runtime. Your server and VM environment have to be setup accordingly.