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?
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 love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With