1

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

Richard H
  • 38,037
  • 37
  • 111
  • 138
  • I see no issue with your edited code. Try debugging through it. – Kolibri Oct 10 '09 at 14:39
  • Hi Kolibri - I have discovered the issue: the exception is being thrown by a separate thread that the ImageIcon constructor spawns - hence I can't catch it. – Richard H Oct 10 '09 at 14:46
  • Hah, what an interesting error. I would never have guessed that the ImageIcon constructor would spawn a new thread. You'll have to make a Thread.UncaughtExceptionHandler to handle that unchecked exception. – Kolibri Oct 10 '09 at 15:33

5 Answers5

1

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

new ImageIcon(new URL(imageUri))
Jonathan Feinberg
  • 44,698
  • 7
  • 80
  • 103
  • 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 H Oct 10 '09 at 14:22
1

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

Jonathan Feinberg
  • 44,698
  • 7
  • 80
  • 103
0

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
  • 18,130
  • 10
  • 48
  • 62
  • 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 H Oct 10 '09 at 14:24
  • 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.jpg Uncaught error fetching image: java.lang.SecurityException at java.lang.SecurityManager.checkPermission(SecurityManager.java:570) at java.lang.SecurityManager.checkConnect(SecurityManager.java:1086) at ..... – Richard H Oct 10 '09 at 14:30
0

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 H
  • 38,037
  • 37
  • 111
  • 138
0

This is an old thread - but I thought of adding an alternative answer since I got it after reaching the same problem and hitting this post.

Since I don't want to add more dependencies to my app (=javax) I used the solution suggested here to get the bitmap and then I used setImageBitmap in this case the SecurityException is caught

Community
  • 1
  • 1
Noa Drach
  • 2,381
  • 3
  • 26
  • 44