我正在尝试处理这些异常。例如,当用户加载无效的XML文件时,抛出SAXParseException并要求他加载另一个文件。
似乎“捕获”在这里不起作用。
这是我的代码:
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
}
}
甚至,这不会工作
catch (JAXBException,SAXParseException,UnmarshalException e)
{
//do something
}
@don robi
这就是我得到的:
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)
答案 0 :(得分:0)
异常表明这是一个编译异常。一旦在XML_parser类中修复了错误,您就应该获得您期望的JAXB行为。
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)