Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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?

like image 586
Mike M. Lin Avatar asked Aug 04 '11 19:08

Mike M. Lin


1 Answers

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.

like image 97
Andreas Krueger Avatar answered Nov 16 '22 10:11

Andreas Krueger