java 异常 Exception 相关 try catch finally 解惑
来源:互联网 发布:麦考瑞大学 知乎 编辑:程序博客网 时间:2024/05/18 01:30
首先我们下看一下 下面的几段代码跟运行结果,并从中总结出几个经典的答案:
1.public class Testabl {
public static void main(String[] args) {
Testabl t=new Testabl();
String s=t.testException();
System.out.println(s);
}
private String testException(){
try {
int a=5/0;
} catch (Exception e) {
System.out.println("出现了异常");
}
return "str";
}
}
输出结果:
出现了异常
str
这说明 程序出现了异常并且已经捕捉到,即执行了cath里面的代码,最后在执行了return 。
2.
public class Testabl {
public static void main(String[] args) {
Testabl t=new Testabl();
String s=t.testException();
System.out.println(s);
}
private String testException(){
try {
int a=5/0;
} catch (ArithmeticException e) {
System.out.println("出现了arithmetic异常");
}catch (Exception e) {
System.out.println("出现了Exception");
}
return "str";
}
}
执行结果:
出现了arithmetic异常
str
说明程序先捕捉到了arithmatic 算术异常之后就不会再执行别的catch.
3.当我们把代码稍微变一下,就是把两个抓异常的换了一下顺序
private String testException(){
try {
int a=5/0;
}catch (Exception e) {
System.out.println("出现了Exception");
}catch (ArithmeticException e) {
System.out.println("出现了arithmetic异常");
}
return "str";
}
这个时候出现了编译错误, Unreachable catch block for ArithmeticException. It is already handled by the catch block for Exception
这说明 catch异常应该从级别小的依次往的级别大的抓,不能抓了大的,再抓小的。
4.
private String testException(){
try {
int a=5/0;
} catch (ArithmeticException e) {
System.out.println("出现了arithmetic异常");
}finally{
System.out.println("执行了fianlly");
}
return "str";
}
输出结果:
出现了arithmetic异常
执行了fianlly
str
这说明 程序执行了catch 然后执行了finally 最后执行了return
5.我们再稍微变一下上面的代码
private String testException(){
try {
int a=5/0;
} catch (ArithmeticException e) {
System.out.println("出现了arithmetic异常");
return "ExcetionStr";
}finally{
System.out.println("执行了fianlly");
}
return "str";
}
执行结果:
出现了arithmetic异常
执行了fianlly
ExcetionStr
这个时候我们纳闷了,为什么是这个顺序,因为finally 是在程序返回之前执行的,也就是先输出 “出现了arithmetic异常 ”,然后再是执行fiannal, 最后让return ExcetionStr,如果是程序执行了return ExcetionStr 再执行finally ,那还有什么意义,程序都已经返回了,也就是说这个方法已经执行完了,所以说 无论怎么样,return是最后一步执行的。
好多面试题都会出这个知识点。
由于比较仓促,笔者暂时只总结了以上几点,如果有疑问可以评论,必定一一回复。
- java 异常 Exception 相关 try catch finally 解惑
- java中的异常(exception)&&try,catch,finally throw&&throws
- java异常-Error和Exception;try,catch,throw,throws,finally
- Java异常处理( try catch finally throw throws exception error)
- try catch finally解惑
- Java 异常: try...catch...finally
- Java解惑之try catch finally
- Java解惑之try catch finally
- Java解惑之try catch finally
- Java解惑之try catch finally
- Java解惑之try catch finally
- java解惑之try catch finally
- Java Exception 之 try catch finally
- java中的异常处理 try-catch 以及try-catch-finally
- 异常try-catch-finally
- Java异常--基本概念try...catch...finally
- java 异常捕捉 ( try catch finally )
- try catch finally java 异常捕捉
- 流形学习
- HDU-2050
- 即将踏上征途
- 软件性能的认识
- 大嘴巴漫谈数据挖掘基础篇-数据挖掘简介
- java 异常 Exception 相关 try catch finally 解惑
- Mac下MySQL使用
- jplogic开发案例之应用皮肤界面管理
- 开源项目汇总
- java线程共享受限资源 解决资源竞争 thinking in java4 21.3
- ::在C++中是什么意思
- 好947 Mybatis 配置resultMap 带参数查询Map 注意selectOne数据库返回结果一条数据库 否则会报错
- Fragment 和 FragmentActivity的使用
- Fragment 和 FragmentActivity的使用(二)