Tom*_*mir 0 java eclipse exception-handling
这是我的代码:
public static void main(String[] args) throws Exception {
Scanner s = new Scanner(new File("story.txt"));
someFunction(s);
}
Run Code Online (Sandbox Code Playgroud)
该story.txt是在项目的根,但没有throws Exception代码不运行.为什么?
你不需要throws Exception,但如果你这样离开就不会有任何伤害.
你需要的是一个throws FileNotFoundException因为Scanner你正在使用的构造函数被声明抛出该异常,并且你没有捕获任何异常.