我只是尝试使用try catch语句,但是由于某些原因,它仅在执行以下操作时有效:
{try {
img = ImageIO.read(originalImageone);
Image one= img.getScaledInstance(512, 512, Image.SCALE_DEFAULT);
imgtwo = ImageIO.read(ogImagetwo);
Image two = imgtwo.getScaledInstance(512, 512, Image.SCALE_DEFAULT);
imgthree = ImageIO.read(Imagethree);
Image three = imgthree.getScaledInstance(512, 512, Image.SCALE_DEFAULT);
imgfour = ImageIO.read(Imagefour);
Image four = imgfour.getScaledInstance(512, 512, Image.SCALE_DEFAULT);
imgfive = ImageIO.read(Imagefive);
Image five = imgfive.getScaledInstance(512, 512, Image.SCALE_DEFAULT);
imgsix = ImageIO.read(Imagesix);
Image six = imgsix.getScaledInstance(512, 512, Image.SCALE_DEFAULT);
imgseven = ImageIO.read(Imageseven);
Image seven = imgseven.getScaledInstance(512, 512, Image.SCALE_DEFAULT);
imgeight = ImageIO.read(Imageeight);
Image eight = imgeight.getScaledInstance(512, 512, Image.SCALE_DEFAULT);
}catch(IOException e){
e.printStackTrace();
}}
代码开头的语句是:
BufferedImage imgeight = null;
因此,出于某种原因,尽管您看到了一个额外的花括号,但上述方法仍然有效。每次尝试删除多余的花括号时,都会出现错误。有人知道为什么吗?
答案 0 :(得分:2)
您显然不小心发现了一个匿名“静态初始化”块的用法。 try和catch是只能在“可执行”代码块(例如方法)(和静态块-代码在构造对象之前运行)中找到的关键字。为了使它清晰明了,请尝试编写以下代码:
BufferedImage img = null;
...
BufferedImage imgeight = null;
static {
try {
...
}
catch (Exception e) {
...
}
}