假设我有一个抛出异常的自定义阅读器对象:
public StationReader {
public StationReader(String inFile) throws FileNotFoundException {
Scanner scan = new Scanner(inFile);
while (scan.hasNextLine() {
// blah blah blah
}
// Finish scanning
scan.close();
}
}
我在另一个类 Tester 中调用 StationReader:
public Tester {
public static void main(String[] args) {
try {
StationReader sReader = new StationReader("i_hate_csv.csv");
} catch (FileNotFoundException e) {
System.out.println("File not found arggghhhhhh");
} finally {
// HOW TO CLOSE SCANNER HERE??
}
}
}
现在让我们想象一下,在扫描这些行时,抛出了一个异常,因此scan.close()永远不会被调用。
在这种情况下,如何关闭扫描仪对象?
HUX布斯
相关分类