try、catch、finally⽤法总结
前⾔
在开发过程中异常处理是经常⽤到的,相信⼤部分使⽤try、catch、finally的只知道try中出现异常catch中会捕获,finally块中代码何时都会执⾏。其实其中还有很多细微的知识点,下⾯我们来学习学习。
try、catch、finally执⾏顺序
try块和catch块中逻辑基本相同。try中出现异常跳转到catch,若catch中出现异常则跳转到finally,try或catch正常执⾏若存在return则先执⾏return的代码并保存返回值信息(基本类型保存值信息,引⽤类型则保存地址信息下⾯会说明)然后执⾏finally,若finally 中出现异常或包含return则执⾏结束,若⽆异常且没有return则会执⾏try或catch中的return或结束。整体执⾏流程如下:
说明与代码展⽰
当try-catch-finally中⽆return时,如果try块中出现异常则进⼊catch中,执⾏完catch中代码后进⼊finally,如果catch中出现异常仍然会执⾏finally代码块,finally块执⾏结束后抛出异常。try块中⽆异常时执⾏完try块直接执⾏finally。
以catch中抛出异常为例,代码如下:
public static int testNoReturn(){
int res =1;
try{
try catch的使用方法res++;
System.out.println("try ======== res:"+res);
int a =1/0;
}catch(Exception e){
res++;
System.out.println("catch ======== res:"+res);
int a =1/0;
}finally{
res++;
System.out.println("finally ======== res:"+res);
}
return res;
}
catch中抛出异常后finally仍然会执⾏,执⾏结束后抛出catch中的异常。执⾏结果如下:
try或catch中存在return 时流程基本⼀致所以合到⼀起讲下。finally中包含return时会覆盖try或catch中的return值,⽽且会覆盖catch中抛出的异常信息。
try或catch包含return,返回值为基本数据类型代码如下:
public static int testTryCatchReturn(){
int res =1;
try{
res++;
System.out.println("try ======== res:"+res);
int a=1/0;
return res;
}catch(Exception e){
res++;
System.out.println("catch ======== res:"+res);
return res;
}finally{
res++;
System.out.println("finally ======== res:"+res);
}
}
执⾏结果如下:
try或catch包含return,返回值为引⽤数据类型代码如下:
public static List testTryCatchReturn1(){
List res =new ArrayList();
try{
res.add(1);
System.out.println("try ======== res:"+res);
int a=1/0;
return res;
}catch(Exception e){
res.add(2);
System.out.println("catch ======== res:"+res);
return res;
}finally{
res.add(3);
System.out.println("finally ======== res:"+res);
}
}
执⾏结果如下:
finally中将引⽤的返回值赋值为null时代码如下:
public static List testTryCatchReturn1(){
List res =new ArrayList();
try{
res.add(1);
System.out.println("try ======== res:"+res);
int a=1/0;
return res;
}catch(Exception e){
res.add(2);
System.out.println("catch ======== res:"+res);
return res;
}finally{
res.add(3);
System.out.println("finally ======== res:"+res);
res =null;
System.out.println("finally ======== res:"+res);
}
}
执⾏结果如下:
try或catch中存在return时会将返回值进⾏保存(基本数据类型直接保存,引⽤数据类型则保存引⽤地址),然后执⾏finally块中的代码,finally块中代码执⾏结束后直接返回。因为引⽤类型返回时保存的是地址,所以修改引⽤对象时返回信息会发⽣变化,但如果赋值为null时地址指向的信息并未发⽣变化所以返回值依然是地址指向的对象。
当finally中存在return时会覆盖try或catch中返回的数据。如果catch中抛出异常时会将该异常覆盖掉。以catch中存在异常的情况为例,代码如下:
public static int testFinallyReturn(){
int res =1;
try{
res++;
System.out.println("try ======== res:"+res);
int a=1/0;
return res;
}catch(Exception e){
res++;
System.out.println("catch ======== res:"+res);
int a =1/0;
return res;
}finally{
res++;
System.out.println("finally ======== res:"+res);
return res;
}
}
执⾏结果如下:
finally的return会覆盖catch或try的return和异常信息。
总结
1、⽆return且未出现异常时try->finally,出现异常时try->catch->finally。
2、try或catch中有return时,返回数据为基本类型则finally代码块执⾏完后不会更改,返回值为引⽤类型,return保存的是引⽤地址,finally块中代码执⾏完会改变返回值。
3、finally中存在return时会覆盖try或catch中的返回值信息,若try或catch中抛出异常也会被finally中的return覆盖。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论