在Java Android上使用外部库的未处理异常类型

时间:2014-05-28 21:51:19

标签: java android exception-handling marshalling throwable

我正在做一个小课程来保存和加载游戏中的信息,直到我编写此代码:

import java.io.FileReader;
import java.io.FileWriter;

import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.Marshaller;
import org.exolab.castor.xml.Unmarshaller;
import org.exolab.castor.xml.ValidationException;

class InformationToStoreLoad{
    public InformationToStoreLoad(){

    }
}

public class GameSaverLoader {

        /*
         * Save the current game
         */
        public void saveXML(){

            FileWriter writer;
            try {
                writer = new FileWriter("save.xml");
                Marshaller.marshal(new InformationToStoreLoad(), writer);
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

             /*
         * Load a game saved before
         */
        public void loadXML()
        {
            try {
                FileReader reader = new FileReader("article.xml");
                InformationToStoreLoad gameToLoad = 
                        (InformationToStoreLoad) Unmarshaller.unmarshal(InformationToStoreLoad.class, reader);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
}

我一直在研究.NET平台,而且我对Java有点不太了解,所以我不确定我是否做得对。要导出我右键单击我的项目的Castor库,我选​​择了Properties,然后选择了Java Build Path,然后选择了库,最后我点击了“Add external JAR”并导出了这个JAR:“castor-1.3.1- xml.jar“

但是,使用marshaller和unmarshaller类时出现以下错误

Unhandled exception type ValidationException

如果我在快速修复中选择“添加捕获子句到周围尝试”,那么我会收到此错误:

No exception of type MarshalException can be thrown; an exception type must be a subclass of Throwable

The method printStackTrace() is undefined for the type MarshalException

No exception of type ValidationException can be thrown; an exception type must be a subclass of Throwable

The method printStackTrace() is undefined for the type ValidationException

同样在快速修复中,我可以选择这样做:

public void saveXML() throws MarshalException, ValidationException{

然后我收到了这个错误:

不能抛出MarshalException类型的异常;异常类型必须是Throwable的子类

不能抛出ValidationException类型的异常;异常类型必须是Throwable的子类

我不知道如何修复任何这些错误,任何人都可以建议我如何解决这个问题?

祝你好运

0 个答案:

没有答案
相关问题