简体   繁体   中英

java io catching exception

I am getting this error:

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)

================== My code:

//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

I'm pretty sure my try and catch block is correct but my program is not printing the 2 quarters value and also the text saying "end of file reached" for some odd reason. =/

您正在捕获IllegalBillException (无论如何),但是您没有捕获EOFException (或者它是超类IOException )。

The problem is that your while condition, which tests for EOF with the null check, doesn't "protect" the stuff after " }// try ", and hence the readObject calls after that point will attempt to read beyond EOF and get the exception.

You need to restructure your logic somehow.

Catching EOFException will make the exception "go away" but will not fix your bug.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM