java - 在 for-each 循环中捕获引发异常的确切项目
问题描述
我需要访问我的子句中的File
变量。我怎样才能做到这一点?f
catch
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 placing the try
/catch
block in you for
loop like this:
for(File f : filesInDir) {
try {
new Scanner(new FileInputStream(f));
} catch (FileNotFoundException e) {
System.out.println("Could not open input file " + f + " for reading.");
}
}
推荐阅读
- powershell - 最佳实践 - 格式化编号列表输出 - PowerShell
- php - 在 codeigniter 中错误提交时不会保留表单值
- django - 在 django 表单的两个字段中将一个字段设为必填
- tfs - 微软测试管理器 2015
- python - 如何从其主设备访问 IntVar 或 StringVar 或 DoubleVar 或 BooleanVar
- emacs - 按光标位置缩进粘贴
- php - php弹性搜索中的日期范围
- c++ - QtSql:绑定不会改变使用 SQLite 的查询
- sql - 我想在 SQL 中创建系列数据
- azure - 在 azure data factory V2 中查找活动不成功