java try{}catch异常之后运行
//代码1 public static void test() throws Exception { throw new Exception("参数越界"); System.out.println("异常后"); //编译错误,「无法访问的语句」 }
//代码2 try{ throw new Exception("参数越界"); }catch(Exception e) { e.printStackTrace(); } System.out.println("异常后");//可以执行
//代码3 if(true) { throw new Exception("参数越界"); } System.out.println("异常后"); //抛出异常,不会执行
//代码4
public static void main(String[] args) { int i =10 + 0; tt(); System.out.println("fefegeg");//不可以执行 } public static void tt(){
throw new Exception("参数越界");
}
//代码5
public static void main(String[] args) { int i =10 + 0; tt(); System.out.println("fefegeg");//可以执行 } public static void tt(){ try{
throw new Exception("参数越界");
}catch (Exception e){ e.printStackTrace(); } }
1.若一段代码前有异常抛出,并且这个异常没有被捕获,这段代码将产生编译时错误「无法访问的语句」。如代码1
2.若一段代码前有异常抛出,并且这个异常被try...catch所捕获,若此时catch语句中没有抛出新的异常,则这段代码能够被执行,否则,同第1条。如代码2
3.若在一个条件语句中抛出异常,则程序能被编译,但后面的语句不会被执行。如代码3
4.调用方法发生异常,本方法之后无法执行。如代码4
5.调用方法发生异常并捕获,本方法后可以继续执行。如代码5