2011-02-24 68 views
1

我想處理這些異常。例如,當用戶加載無效的XML文件時,會拋出SAXParseException,並要求他加載另一個文件。SAXParseException和UnmarshalException

看來「catch」在這裏不起作用。

這裏是我的代碼:

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

它以什麼方式不起作用?你看到的行爲是什麼? – 2011-02-24 02:34:21

回答

0

的異常指示這是一個com除外。一旦錯誤在您的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) 
相關問題