欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

java拾遗-java异常机制,

来源: javaer 分享于  点击 17971 次 点评:263

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 去破坏原本的结构。

相关文章

    暂无相关文章
相关栏目:

用户点评