gpt4 book ai didi

java - 捕获在 for-each 循环中抛出异常的确切项目

转载 作者:行者123 更新时间:2023-11-30 05:43:37 24 4
gpt4 key购买 nike

我需要访问 catch 子句中的 File 变量 f。我怎样才能实现这个目标?

try { 
for(File f:filesInDir) {
new Scanner(new FileInputStream(f));
}
catch(FileNotFoundException e) {
System.out.println("Could not open input file "+ f +" for reading.");
}

最佳答案

尝试将 try/catch block 放入 for 循环中,如下所示:

for(File f : filesInDir) {
try {
new Scanner(new FileInputStream(f));
} catch (FileNotFoundException e) {
System.out.println("Could not open input file " + f + " for reading.");
}
}

关于java - 捕获在 for-each 循环中抛出异常的确切项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55226046/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com