为什么此代码会抛出 IOException?
Why does this code throw an IOException?
为什么这段代码抛出 IOException
?
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
try (Scanner scan1 = new Scanner(new File("File1.txt"));
Scanner scan2 = new Scanner(new File("File2.txt"))) {
} catch (IOException e) {
System.out.println("An IOException has been thrown.");
}
System.out.println("Done!");
}
}
我是从书上学来的,但不明白为什么会抛出异常。感谢您的帮助!
因为构造函数 Scanner(File)
抛出 FileNotFoundException
,它是 IOException
的子 class。查看 javadoc 了解更多 details.
为什么这段代码抛出 IOException
?
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
try (Scanner scan1 = new Scanner(new File("File1.txt"));
Scanner scan2 = new Scanner(new File("File2.txt"))) {
} catch (IOException e) {
System.out.println("An IOException has been thrown.");
}
System.out.println("Done!");
}
}
我是从书上学来的,但不明白为什么会抛出异常。感谢您的帮助!
因为构造函数 Scanner(File)
抛出 FileNotFoundException
,它是 IOException
的子 class。查看 javadoc 了解更多 details.