答案是4号,
4.- The main method should simply terminate if any exception occurs.
throws子句仅表示该方法抛出一个已检查的FileNotFoundException,调用方法应该捕获或重新抛出它。如果在main方法中抛出一个未被检查的异常(而不是catch),它也将被终止。
检查这个测试:
public class ExceptionThrownTest {
@Test
public void testingExceptions() {
try {
ExceptionThrownTest.main(new String[] {});
} catch (Throwable e) {
assertTrue(e instanceof RuntimeException);
}
}
public static void main(String[] args) throws FileNotFoundException {
dangerousMethod();
// Won't be executed because RuntimeException thrown
unreachableMethod();
}
private static void dangerousMethod() {
throw new RuntimeException();
}
private static void unreachableMethod() {
System.out.println("Won't execute");
}
}
正如你所看到的,如果我抛出一个RuntimeException,即使抛出的异常不是FileNotFoundException,该方法也将终止