如果异常中没有finally,那么直接抛出异常,因为抛出异常后实际上就会有“return”结束后面代码的执行。
如果异常中有finally,那么finally里面的内容是可以被继续执行的,执行完之后才会返回。
但最终结果是try大括号后面的内容肯定不会被执行。
举例:
public class Test {
public static void main(String args[]) {
OutputStream os = null;
try {
os = new FileOutputStream(new File(""));
} catch (IOException e) {
System.out.println("被执行1");
} catch (Exception e) {
System.out.println("被执行2");
} finally {// 此处不管是否有异常,都会被执行
System.out.println("被执行3");
if (os != null) {
try {
os.close();
} catch (Exception e) {
}
}
}
}
}