异常03

作者: 哈迪斯Java | 来源:发表于2021-11-11 16:38 被阅读0次

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 配合使用,这种用法相当于没有捕获异常,因此程序会直接崩掉/退出。应用场景,就是执行一段代码,不管是否发生异常,都必须执行某个业务逻辑

相关文章

网友评论

      本文标题:异常03

      本文链接:https://www.haomeiwen.com/subject/gavizltx.html