如何捕获 java 未经检查/运行时异常(特别是 SecurityException)

发布于 2024-08-07 13:16:05 字数 861 浏览 5 评论 0原文

我有一个 java 类,它有一个从网站获取图像的方法:

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;
}

问题是有时我尝试获取的 imageUri 会被重定向,导致 ImageIcon 构造函数抛出 java.lang.SecurityException - 它未被 catch 子句捕获,导致我的程序终止。

谁能建议我如何捕捉这个异常?

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

变身佩奇 2024-08-14 13:16:05

该异常是由构造函数引发的,该构造函数未包装在 try 块中。

new ImageIcon(new URL(imageUri))

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

new ImageIcon(new URL(imageUri))
静若繁花 2024-08-14 13:16:05

使用 ImageIcon 加载图像是 1998 年的事情。您想要 ImageIO.read()

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

夏有森光若流苏 2024-08-14 13:16:05

如果异常确实是从 getImage() 引发的,您的代码应该捕获它。 SecurityException 是异常。你有什么地方搞错了。例如,将 ImageIcon 构造函数放在 try 下。如果没有帮助,请尝试,

catch( Throwable th )

但这是一个坏习惯。在记录它之后至少尝试重新抛出它(或包装器异常)。

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.

彼岸花似海 2024-08-14 13:16:05

由于 ImageIcon 非常老派,并且产生了一个新线程(我不想要),我的解决方案如下:

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;
}

任何重定向、死链接等问题现在都会得到妥善处理。

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.

月野兔 2024-08-14 13:16:05

这是一个旧线程 - 但我想添加一个替代答案,因为我在遇到同样的问题并点击这篇文章后得到了它。

因为我不想向我的应用程序添加更多依赖项(= javax)
我使用此处建议的解决方案来获取位图,然后在本例中使用setImageBitmap SecurityException 被捕获

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文