2017-11-25 297 views
1

这是一个图像处理应用程序。我在下面显示的代码是用于创建一个图像文件并将完整处理的BufferedImage保存到它。尝试创建映像文件时,try-catch无法捕获IOException

public static void saveAnh(BufferedImage anhHoanTat) { 
    String dc; 
    ui.save(); 
    input.nextLine(); 
    diachiluuanh = input.nextLine(); 
    dc = diachiluuanh 
     + diachi.substring(diachi.lastIndexOf("\\"), diachi.lastIndexOf(".")) 
     + "_ML."+ diachi.substring(diachi.lastIndexOf(".")+1); 
    File anhDaXuLy = new File(dc); 
    try { 
     ImageIO.write(anhHoanTat,diachi.substring(diachi.lastIndexOf(".")+1), anhDaXuLy); 
    } catch (IOException e) { 
     ui.warningSave(); 
    } 
    ui.hoanTat(dc); 
} 

一切工作正常,但它没有捕获IOException。系统显示错误,它是FileNotFoundException,据我所知,异常也是IOException

什么样的系统显示截图:

Screenshot of what the system showed

然后我试图抓住一个确切catch (FileNotFoundException e),但随后的Eclipse会让我改回IOException。是Eclipse促使我

截图:

Screenshot of what Eclipse prompted me

Screenshot of what Eclipse prompted me

(它告诉我,FileNotFoundException已经由IOException抓住了,所以最终我不得不删除这几乎回去到我开始的地方)。

注意:我在那之后加了NullPointerException和代码抓住它,但还是没有赶上什么系统显示IOExceptioncatch (NullPointerException | IOException e)

截图:

Screenshot of what the system showed

回答

1

会发生什么事是你正确捕获异常,你打印出来(我假设这就是你在ui.warningSave();方法中所做的),但是然后你不停止你的方法(或者返回,退出,抛出异常),所以程序到达最后一行后,catch(ui.hoanTat(dc);

编译错误日食sho请你:

因为ImageIO.write()抛出IOException,你不能只捕获FileNotFoundException,因为它不包括所有情况。

此外,编写catch (FileNotFoundException | IOException e)也是错误的,因为FileNotFoundException是多余的 - 它在扩展它时已经被IOException覆盖。

+0

虽然仍然无法正常工作。 –

相关问题