Ignoring DatatypeConfigurationException when creating a new XMLGregorianCalendar

When creating a new XMLGregorianCalendar instance like this, do I really need to handle the DatatypeConfigurationException 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 crude logic says that this will not throw an exception unless I give it a bad input. And this cannot be in the above example. This is what JavaDocs are talking about:

If the system property is specified by DATATYPEFACTORY_PROPERTY , "javax.xml.datatype.DatatypeFactory", there is a class named property value is created. Any exception thrown during the instantiation process is wrapped as a DatatypeConfigurationException .

Did I understand correctly that I can safely suppress this checked exception?

+6
source share
2 answers

An exception of type DatatypeConfigurationException can occur only when a static method is called

 DataTypeFatory factory = DataTypeFactory.newInstance(); 

Therefore, you should consider it only once . But you have to process it once , otherwise you will not be able to instantiate XMLGregorianCalendar at all.

Clear call

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

never throws a DatatypeConfigurationException , so you don't need to handle it when creating the XML views of your GregorianCalendar instances. - As from the Java SE API in the last call, only NullPointerException can appear.

+5
source

If you disable this exception, you will not set the header request time. In this regard, yes, this is a serious exception. . You cannot generate XML representations of your date and time instances.

On the other hand, your program will not fail. This is not a critical VM error. But you have to deal with it. You cannot fix this at runtime. Therefore, you must configure the server and VM environment.

0
source

Source: https://habr.com/ru/post/894327/


All Articles