SAXParseException and UnmarshalException
I am trying to deal with these exception. For example when a user, loads an invalid XML file, then SAXParseException is thrown and he is asked to load another file.
it seems that "catch" won't work here.
here's my code:
public void parseXML_FROM_file (File xml_file)
{
try {
JAXBContext jc = JAXBContext.newInstance ("generated");
//Creating an Unmarshaller.
Unmarshaller u = jc.createUnmarshaller ();
//USING FILE APPROACH
System.out.println("Using FILE approach:");
JAXBElement element = (JAXBElement) u.unmarshal(xml_file);
TEST_Class mainTest = (TEST_Class) element.getValue ();
} catch (JAXBException e)
{
e.printStackTrace ();
}catch (SAXParseException e)
//do something
}catch (UnmarshalException e)
//do something
}
}
even, this wont work
catch (JAXBException,SAXParseException,UnmarshalException e)
{
//do something
}
@don robi
this is what i get:
Exception in 开发者_运维百科thread "main" java.lang.Error: Unresolved compilation problems:
The type JAXBException is not generic; it cannot be parameterized with arguments <SAXParseException, UnmarshalException>
Syntax error on token ",", < expected
Syntax error, insert ">" to complete ReferenceType1
at XML_Parser.parseXML_FROM_file(XML_Parser.java:64)
at Main_Class.main(Main_Class.java:13)
The exception indicates that this is a compilation exception. Once the errors are fixed in your XML_parser class you should get the JAXB behaviour you are expecting.
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The type JAXBException is not generic; it cannot be parameterized with arguments <SAXParseException, UnmarshalException>
Syntax error on token ",", < expected
Syntax error, insert ">" to complete ReferenceType1
at XML_Parser.parseXML_FROM_file(XML_Parser.java:64)
at Main_Class.main(Main_Class.java:13)
精彩评论