如何捕获 java 未经检查/运行时异常(特别是 SecurityException)
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
该异常是由构造函数引发的,该构造函数未包装在 try 块中。
The exception is being thrown by the constructor, which is not wrapped in the try block.
使用 ImageIcon 加载图像是 1998 年的事情。您想要 ImageIO.read()。
Using ImageIcon to load an image is sooooo 1998. You want ImageIO.read().
如果异常确实是从 getImage() 引发的,您的代码应该捕获它。 SecurityException 是异常。你有什么地方搞错了。例如,将 ImageIcon 构造函数放在 try 下。如果没有帮助,请尝试,
但这是一个坏习惯。在记录它之后至少尝试重新抛出它(或包装器异常)。
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
It's a bad habit though. Try at least to re-throw it (or a wrapper exception) after logging it.
由于 ImageIcon 非常老派,并且产生了一个新线程(我不想要),我的解决方案如下:
任何重定向、死链接等问题现在都会得到妥善处理。
Due to ImageIcon being extremely old school, and spawning a new thread (which i do not want) , my solution is as follows:
Any issues with redirects, dead links etc are now handled gracefully.
这是一个旧线程 - 但我想添加一个替代答案,因为我在遇到同样的问题并点击这篇文章后得到了它。
因为我不想向我的应用程序添加更多依赖项(= 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