Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Exception Handling Code Doesn't Work

 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

The following is from Chapter 5, Sierra/Bates



I created myfile.txt in the same directory, and now I receive the following error:

IO Error
java.io.EOFException
java.io.EOFException
at java.io.RandomAccessFile.readFully(RandomAccessFile.java:383)
at ReadData.main(ReadData.java:8)



Two questions:
1. How do I fix this error?
2. Why does FileNotFoundException handler say, System.err.println(e.getMessage()), while IOException handler says System.err.println(e.toString());?
 
Ranch Hand
Posts: 814
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Sandra

Would you try using following code at line number 9 and if you try then post output of code

I think file should have minimum 1000 bytes to read other wise EOF will be thrown.
see this
 
Ranch Hand
Posts: 437
Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Sandra.

1. How do I fix this error?



Increase the size of 'myfile.txt' file upto 1000 bytes. I think this will fix the error.
 
Sheriff
Posts: 9674
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Chinmaya Chowdary wrote:Increase the size of 'myfile.txt' file upto 1000 bytes. I think this will fix the error.


It won't solve the problem. Ninad is correct. Since Sandra is using readFully the call throws exception if the file ends before reading 1000 bytes. Instead of readFully use read(byte[]) method...
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I replaced readFully with read and now the program compiles and runs.

Thanks!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic