Java的IO异常醒目

我得到这个错误:

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

我敢肯定,我尝试和catch块是正确的,但我的程序不打印的2个季度值,也是文说:“文件的末尾达到了”一些奇怪的原因。 = /

--------------解决方案-------------

你捉IllegalBillException (不管它是什么),但你不要再追EOFException (或它的父类, IOException )。

问题是,你while条件,测试对于与空检查EOF,没有“保护”的东西后,“ }// try ”,因此readObject调用后,该点会试图读取超出EOF并获得例外。

你需要以某种方式调整你的逻辑。

醒目EOFException将使例外“走开”,但不会解决您的错误。

分类:java的 时间:2012-10-10 人气:0
本文关键词: Java中,例外,尝试捕捉
分享到:

相关文章

Copyright (C) 55228885.com, All Rights Reserved.

55228885 版权所有 京ICP备15002868号

processed in 0.407 (s). 9 q(s)