异常常见类型处理分析

package cyb.lxf.staring.test;
public class TestMyException {
public static void main(String[] args) {
TestMyException.ex5();
}
public static void ex1() {
try {
int tmp = 1 / 0;
} catch (Exception e) {
System.out.println("ex occur!");
} finally {
System.out.println("finally hadle...");
}
/**
* ex occur!
* finally hadle...
*/
}
public static void ex2() {
try {
int tmp = 1 / 0;
System.out.println("after ex.");//异常后的语句不会执行
return ;
} catch (Exception e) {
System.out.println("ex occur!");
} finally {
System.out.println("finally hadle...");
}
/**
* ex occur!
* finally hadle...
*/
}
public static void ex3() {
try {
int tmp = 1 / 0;
System.out.println("after ex.");//异常后的语句不会执行
return ;
} catch (Exception e) {
System.exit(0);
System.out.println("ex occur!");
} finally {
System.out.println("finally hadle...");
}
/**
* 无输出

*/
}
public static void ex4() {
try {
int tmp = 1 / 0;
System.out.println("after ex.");//异常后的语句不会执行
return ;
} catch (Exception e) {
System.out.println("ex occur!");
System.exit(0);//程序非、正常退出,导致finally无法正常处理
} finally {
System.out.println("finally hadle...");
}
/**
* ex occur!

*/
}
public static void ex5() {
try {
int tmp = 1 / 0;
System.out.println("after ex.");//异常后的语句不会执行
return ;
} catch (Exception e) {
System.out.println("ex occur!");
return ;//不会影响finally无法正常处理
} finally {
System.out.println("finally hadle...");
}
/**
* ex occur!
* finally hadle...
*/
}
public static void ex6() {
try {
int tmp = 1 / 0;
System.out.println("after ex.");//异常后的语句不会执行
return ;
} catch (Exception e) {
System.out.println("ex occur!");
return ;//不会影响finally无法正常处理
//System.out.println("ex occur!");//语法不通过,return 后的语句永远不会被执行到。
} finally {
return ;
//System.out.println("finally hadle...");//语法不通过,return 后的语句永远不会被执行到。
}
/**
* ex occur!
* finally hadle...
*/
}
//任何单位语句块中,return后边的语句都不能被执行。
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值