java拾遗-java异常机制

这里写图片描述
如图所示,我们众所周知的异常处理机制。java的异常机制看似简单,但里面存在许许多多我们意想不到的小bug。查了文章等相关资料,做一下总结整理。

1.少使用受检异常,多用java异常体系标准异常
会花费很多精力去抓住它–effective java。具体原因可以看书。
学会使用:
IllegalArgumentException:错误参数
IllegalStateException:错误状态
NullPointerException:空指针
IndexOutOfBoundsException:指针越界
UnsupportedOperationException:不支持用户操作

2.不管有没有出现异常,finally块中代码都会执行

public class TestException {
    public void test1(){
        try {
            this.test2();
        } catch (Exception e) {

        }finally{
            System.out.println("test 1 finally");
        }
    }

    public void test2() throws Exception{
        try{

        }catch(Exception e){

        }finally{
            System.out.println("test 2 finally");
        }
    }
    public static void main(String[] args) {
        TestException testException1 = new TestException();
        testException1.test1();
    }
}

结果:

test 2 finally
test 1 finally

3.finally无法改变原本返回值
当在try块或catch块中遇到return语句时,finally语句块将在方法返回之前被执行,此时并没有返回运算后的值,而是先把要返回的值保存起来,管finally中的代码怎么样,返回的值都不会改变,任然是之前保存的值),所以函数返回值是在finally执行前确定的;

    public int test1(){
        int i = 0;
        try {
            return i;
        } catch (Exception e) {

        }finally{
            i = 1;
            System.out.println("test 1 finally");
        }
        return i;
    }
        public static void main(String[] args) {
        TestException testException1 = new TestException();
        int i =testException1.test1();
        System.out.println(i);
    }

结果:

test 1 finally
0//并没有变成1

4.finally里面的return很危险
finally中最好不要包含return,否则程序会提前退出,返回值不是try或catch中保存的返回值。如果finally中有return语句,那么程序就return了,所以finally中的return是一定会被return的,编译器把finally中的return实现为一个warning。

例子一:
参考并对比3的代码

    public int test1(){
        int i = 0;
        try {
            return i;
        } catch (Exception e) {

        }finally{
            i = 1;
            System.out.println("test 1 finally");
            return i;//在这里就返回掉
        }

    }
    public static void main(String[] args) {
        TestException testException1 = new TestException();
        int i =testException1.test1();
        System.out.println(i);
    }

结果:

test 1 finally
1//变成1了 恐怖把

这里try 里面的返回被finally截断了,所以会导致数据异常,这时我们的编译器就会给我们爆出了一个提醒
这里写图片描述
我们要注意这样的用法

例子二:
截断throw,其实有点等同于return

public class TestException {
    public void test1(){
        try {
            test2();
        } catch (Exception e) {
            System.out.println("test 1 catch");
        }finally{
            System.out.println("test 1 finally");
        }

    }

    public void test2() throws Exception{
        try{
            int c=0;
            c=c/0;
        }catch(Exception e){
            System.out.println("test 2 catch");
            throw e;
        }finally{
            System.out.println("test 2 finally");
        }
    }
    public static void main(String[] args) {
        TestException testException1 = new TestException();
        testException1.test1();
    }
}

结果:

test 2 catch
test 2 finally
test 1 catch
test 1 finally

我们在test2 finally中加入return 得到:

/**
 * 
 */
package com;

/**
 * @author jiangjintai
 *
 */
public class TestException {
    public void test1(){
        try {
            test2();
        } catch (Exception e) {
            System.out.println("test1 catch");
        }finally{
            System.out.println("test 1 finally");
        }

    }

    public void test2() throws Exception{
        try{
            int c=0;
            c=c/0;
        }catch(Exception e){
            System.out.println("test 2 catch");
            throw e;
        }finally{
            System.out.println("test 2 finally");
            return ;//加入奇异代码
        }
    }
    public static void main(String[] args) {
        TestException testException1 = new TestException();
        testException1.test1();
    }
}

结果:

test 2 catch
test 2 finally
test 1 finally

test 1 的catch 并没有拦截到异常,也就是说finally拦截了原本应该抛出的异常。

over all of that , 当使用try - catch -finally 的时候,要留意finally,他是一定会执行的,且不要在finally里面加return 去破坏原本的结构。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值