When I undo the Xml node on a property, setMyProperty is called. Property setter code may throw an exception: what happens in this case?
The behavior that I observed: if an exception is an uncontrolled exception, then it is swallowed by jaxb "internal" and this property is ignored. If the RuntimeException is thrown into an Exception (therefore marked and added to the throws clause of the property setting tool), this causes the marshal to fail.
Question: Is this behavior you can rely on? thanks in advance Agostino
PS: Ok, “swallowed” - this is not the right word, because in fact it is controlled “in the best way”, correctly rejecting the rest of the document. However, the unmarshall caller is not notified that an exception has occurred.
PS: a simple test case, just in case :-)
package test.jaxb; import java.io.File; import javax.xml.bind.*; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement public class SZL { public static void main(String [] args) throws JAXBException { SZL test = new SZL(); test.myProp = "test-value"; test.setMyProp2("test-value-2"); JAXBContext jc = JAXBContext.newInstance(SZL.class); File f = new File("SZL.xml"); Marshaller m = jc.createMarshaller(); m.marshal(test, f); test = null; Unmarshaller um = jc.createUnmarshaller(); test = (SZL)um.unmarshal(f); System.out.println("The RuntimeException has been swallowed"); System.out.println(test.myProp); System.out.println(test.myProp2); } private String myProp; public void setMyProp(String myProp) { throw new RuntimeException();
source share