views:

523

answers:

5

Hi all,

I have java class with a method which gets an image from a website:

private Image image;
private int height;
private int width;
private String imageUri;

public Image getImage() {
 if (image == null) {
  log.info("Fetching image: " + imageUri);
  try {
   URL iURL = new URL(imageUri);
   ImageIcon ii = new ImageIcon(iURL);
   image = ii.getImage();
   height = image.getHeight(null);
   width = image.getWidth(null);
  } catch (SecurityException e) {
   log.error("Unable to fetch image: " + imageUri,e);
  } catch (MalformedURLException e) {
   log.error("Unable to fetch image: " + imageUri,e);
  }
 }
 return image;
}

The problem is that sometimes the imageUri I try to fetch gets redirected, causing the ImageIcon constructor to throw a java.lang.SecurityException - which is not caught by the catch clause, causing my program to terminate.

Can anyone suggest how I might catch this exception?

Thanks

A: 

If the exception indeed thrown from getImage(), your code should catch it. SecurityException is Exception. You've got it wrong somewhere. For instance, place ImageIcon constructor under try. If it doesn't help, try

catch( Throwable th )

It's a bad habit though. Try at least to re-throw it (or a wrapper exception) after logging it.

Vladimir Dyuzhev
Hi - in fact is was the ImageIcon constructor throwing the exception, I have updated my code to include it in the catch. However - it's still not caught! Trying your suggestion.
Richard
Hi - I tried catching the Throwable, but to no avail.... I still get:2009-10-10 15:29:02,301 [main] INFO images.ImageData - Fetching image: http://www.*******.com/images/bg.jpgUncaught error fetching image:java.lang.SecurityException at java.lang.SecurityManager.checkPermission(SecurityManager.java:570) at java.lang.SecurityManager.checkConnect(SecurityManager.java:1086) at .....
Richard
+1  A: 

The exception is being thrown by the constructor, which is not wrapped in the try block.

new ImageIcon(new URL(imageUri))
Jonathan Feinberg
Hi Jonathan, you are absolutely correct - i just ran it through the debugger. Am still left with the same problem though - exception is not being caught - am editing my code to reflect new attempt at catch.
Richard
+1  A: 

I have discovered the issue: the ImageIcon constructor spawns a separate thread, and the exception is being thrown from there, hence I cannot catch it.

Richard
A: 

Using ImageIcon to load an image is sooooo 1998. You want ImageIO.read().

Jonathan Feinberg
Thanks for suggestion, due to ImageIcon spawning a seperate thread I am now doing exactly that.
Richard
A: 

Due to ImageIcon being extremely old school, and spawning a new thread (which i do not want) , my solution is as follows:

public Image getImage() {
 if (image == null) {
  log.info("Fetching image: " + imageUri);
  try { 
   URL iURL = new URL(imageUri);
   InputStream is = new BufferedInputStream(iURL.openStream());
   image = ImageIO.read(is);
   height = image.getHeight();
   width = image.getWidth();
  } catch (MalformedURLException e) {
   log.error("Unable to fetch image: " + imageUri, e);
  } catch (IOException e) {
   log.error("Unable to fetch image: " + imageUri, e);
  }
 }
 return image;
}

Any issues with redirects, dead links etc are now handled gracefully.

Richard