开发者

Ignoring DatatypeConfigurationException when creating new XMLGregorianCalendar

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 w开发者_StackOverflow社区ouldn'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?


The exception of type DatatypeConfigurationException may happen only in the static method call

DataTypeFatory factory = DataTypeFactory.newInstance();

Therefore 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

XMLGregorianCalendar xmlCal = factory.newXMLGregorianCalendar(new GregorianCalendar());

never throws a DatatypeConfigurationException, thus you don't have to treat it, when creating XML representations of your GregorianCalendar instances. - As from the Java SE API in the latter call only a NullPointerException can occur.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜