【问题标题】:java io catching exceptionjava io捕获异常
【发布时间】:2011-12-18 21:13:48
【问题描述】:

我收到此错误:

bill value:$ 0.10
bill value: $0.05
bill value: $0.01
bill value: $100.00
Exception in thread "main" java.io.EOFException
    at java.io.ObjectInputStream$BlockDataInputStream.peekByte(Unknown Source)
    at java.io.ObjectInputStream.readObject0(Unknown Source)
    at java.io.ObjectInputStream.readObject(Unknown Source)
    at ReadMoney.main(ReadMoney.java:12)

================== 我的代码:

//import java.util.Date;
public class ReadMoney
{
    public static void main(String argv[]) throws Exception
    {
        FileInputStream fis = new FileInputStream("money.out");
        ObjectInputStream ois = new ObjectInputStream(fis);
        Object read;
        try
        {
            while ((read = ois.readObject()) != null)
            {
                if (read instanceof Bill)
                {
                    System.out.println("bill value: " + read);
                }
                else if (read instanceof Quarter)
                {
                }// while
                else if (read instanceof Dime)
                {
                    System.out.println("bill value:" + read);
                }
                else if (read instanceof Nickel)
                {
                    System.out.println("bill value:" + read);
                }
                else if (read instanceof Penny)
                {
                    System.out.println("bill value:" + read);
                }
                else if (read instanceof Bill)
                {
                    System.out.println("bill value:" + read );
                }

            Money quarter = (Money) ois.readObject();
            System.out.println("Quarter: "+ quarter);
            System.out.println("Quarter: "+ quarter);
            Money dime = (Money) ois.readObject();
            System.out.println("Dime:" + dime);
            Money nickel = (Money)ois.readObject();
            System.out.println("Nickel:" + nickel);
            Money penny = (Money) ois.readObject();
            System.out.println("Penny:" + penny);
            Money bill = (Money) ois.readObject();
            System.out.println("Bill: " + bill);
        }// try
        } catch (IllegalBillException ibe)
        {
            System.out.println("End of file reached");
        }
        ois.close();
        fis.close();
    }// main
}// class

我很确定我的 try and catch 块是正确的,但是由于某些奇怪的原因,我的程序没有打印 2 个季度的值以及“到达文件结尾”的文本。 =/

【问题讨论】:

  • 你的文件内容是什么?似乎它不包含您认为的内容。

标签: java exception io try-catch


【解决方案1】:

你正在捕捉IllegalBillException(不管是什么),但你没有捕捉到EOFException(或者它是超类,IOException)。

【讨论】:

  • 哦,我明白了,谢谢!我错过了另一个 catch 语句!谢谢各位!
【解决方案2】:

问题是您的 while 条件(使用 null 检查测试 EOF)不会“保护”“}// try”之后的内容,因此在该点之后的 readObject 调用将尝试读取 EOF 之外的内容并获取异常。

你需要以某种方式重构你的逻辑。

捕获EOFException 将使异常“消失”,但不会修复您的错误。

【讨论】:

  • 感谢热舔!现在已经修好了! :)
猜你喜欢
  • 2013-06-26
  • 1970-01-01
  • 2016-03-10
  • 2010-11-25
  • 2012-10-26
  • 1970-01-01
  • 2017-06-14
  • 2015-07-04
  • 2021-12-13
相关资源
最近更新 更多