views:

15

answers:

1

When using imageio.imageio.read iget a can't create ImageInputStream. I have a catch exception around it so the program survives but i was wondering if theres a way to put an if statement round it that checks to see if it falied and then attempt to read it again if it did. basically asking if there is a test for exceptions?

+1  A: 

try...catch is the test for exceptions. If you really want to treat your exception as a loop control mechanism, you can wrap it up something like this:

boolean success = false;
do {
   try {
      // do imageIO stuff
      success = true;      // this statement only reached if no exception
   } catch (Exception e) {
      System.err.println(e);
   }
} while (!success);

As doublep hints, this is a pretty senseless implementation because it's unlikely for the problem to go away from one iteration of the loop to the next, so your program will probably just loop endlessly printing error messages.

Carl Smotricz
This is exactly the thing I was after thanks. This is reading a camera image that is being saved from a connected dslr camera and the problem is that i attempt to read it before the whole image has been written so the delay between reads should allow time for the whole image to be written before it is attempted to be read again.
pie154
Ah OK. I was afraid you might be doing something like that :) A more careful implementation might periodically check the file size and defer loading the image until the size stabilizes. The approach of crashing through your errors is a bit brute-force but if it works for you, there's no need to make it more complicated :)
Carl Smotricz