异常常见类型处理分析

来源:互联网 发布:mac给iphone刷机 编辑:程序博客网 时间:2024/06/01 08:40
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 0