为什么要在catch块里写状态终止代码

时间:2021-10-08 10:28:25
public class Test2 {
public static String doit() {
try {
BufferedInputStream bufferedInputStream = new BufferedInputStream(
new FileInputStream(new File("i9i9")));
} catch (FileNotFoundException e) {
return "exception";
}
return "success";
}
public static void main(String[] args) {
System.out.println(doit());
System.out.println("going to run1");
System.out.println("going to run2");
System.out.println("going to run3");

}
}

输出结果:
exception
going to run1
going to run2
going to run3

也就是说如果不在catch块里做异return或者throw的话,遇到异常程序是会继续走下去的,可能会造成严重错误。一般来说catch块里肯定要写一些其他的处理代码,但是最后一定是以return throw结尾的。