您的位置:首页 > 其它

异常常见类型处理分析

2015-01-08 17:22 357 查看
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后边的语句都不能被执行。

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: