• Post Reply Bookmark Topic Watch Topic
  • New Topic

FileNotFoundException not being caught

 
Garry Stanway
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am using ImageIO to write a BufferedImage I have aquired from a scanner using some native code out to a .jpg file. I have surrounded the call to ImageIO.write(..) with a try/catch block but if the path I use is wrong (I am getting the path from a properties file as a string), the FileNotFoundException isn't caught but propagates all the way up the stack and out so I can't deal with it.

I have even tried to specifically catch the FileNotFoundException rather than the superclass IOException.

Any ideas?

Garry Stanway



[ November 02, 2007: Message edited by: Garry Stanway ]
[ November 02, 2007: Message edited by: Garry Stanway ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's very weird. Seems like one of the "obvious" assumptions here is incorrect.

Are you sure the stack trace points to that exact line, the ImageIO.write()?

Are you sure that it's just a FileNotFoundException on its own, not SomeOtherException which was caused by FileNotFoundException? Check the log carefully to make sure that the FNF isn't just part of a larger stack trace.

Are you sure that the .java file you're looking at is the one that's being compiled and executed? It could be that there's an older .class file in your classpath which is not catching the FNFE.

Try adding print statements just before and after the ImageIO.write(). This will tell you (a) whether your most recent changes are really affecting the results, (b) whether the line you're looking at is getting executed at all, and (c) whether that line is exiting normally, or some exception/throwable (not necessarily FNFE) is preventing the second print statement from executing. That way you can check the validity of your interpretation of the stack trace.
 
Garry Stanway
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim,

I changed a couple of lines, cleaned and rebuilt - Netbeans 5.5.1

Looking at the log it appears that the FNF is coming from a java.io.RandomAccessFile.open(Native Method) call. Is this a bug? I haven't tried to compile and run with a different JDK. I am using 1.6.0_02. I am about to go to bed. I might try to hand compile and run with an earlier JDK. Below is my changed code and the error stack.

Garry



 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aha! Here we are:

There are two exceptions here. Why is that? I followed the stack trace for the first exception, starting at your call to ImageIO.write(). I followed it into source code (I lucked out that you seem to be using JDK 6, same as me, so the line numbers were the same. When I got to FileImageOutputStreamSpi.java line 37, I found this:

What happens here is that new FileImageOutputStream() throws a FileNotFoundException, but some Sun programmer went and caught the exception, printed the stack trace, and returned null. Which is why it's no longer possible to catch the FileNotFoundException - it's already printed. Shortly afterwards, the returned null value causes a NullPointerException, which is what it being thrown from the method you called. I also found this bug report, so Sun is aware of the problem. Note that the bug has been there since JDK 1.4, so don't expect them to fix it anytime soon. Sigh.

You could catch the NullPointerException, but you'll still get the NileNotFoundException printed confusingly in your log. Probably your best bet here is to check for the existence of the file before you try to call ImageIO.write(). Or maybe you ahsould just check that the directory, is valid, e.g.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!