package chapter12.Exception_;
public class Demon04 {
public static void main(String[] args) {
try {
int b=12;
int c=12;
System.out.println(b/c);
} finally {
System.out.println("执行了finally...");
}
System.out.println("程序继续在执行.......");
}
}
输出
1
执行了finally...
程序继续在执行.......
=============
package chapter12.Exception_;
public class Demon04 {
public static void main(String[] args) {
try {
int b=12;
int c=0;
System.out.println(b/c);
} finally {
System.out.println("执行了finally...");
}
System.out.println("程序继续在执行.......");
}
}
输出
执行了finally...
Exception in thread "main" java.lang.ArithmeticException: / by zero
at chapter12.Exception_.Demon04.main(Demon04.java:8)
5)可以进行 try-finally 配合使用,这种用法相当于没有捕获异常,因此程序会直接崩掉/退出。应用场景,就是执行一段代码,不管是否发生异常,都必须执行某个业务逻辑
网友评论