首页 课程 师资 教程 报名

JAVA中异常与异常处理详解 (二)

  • 2019-09-11 11:54:07
  • 1821次 动力节点


动力节点java培训机构小编分享的“JAVA中异常与异常处理详解”的内容太长,请看上文:http://www.bjpowernode.com/javazixun/1828.html


Java自定义异常


  如果要自定义异常类,则扩展Exception类即可,因此这样的自定义异常都属于检查异常(checked exception)。如果要自定义非检查异常,则扩展自RuntimeException。


  按照国际惯例,自定义的异常应该总是包含如下的构造函数:


  一个无参构造函数


  一个带有String参数的构造函数,并传递给父类的构造函数。


  一个带有String参数和Throwable参数,并都传递给父类构造函数


  一个带有Throwable 参数的构造函数,并传递给父类的构造函数。


  下面是IOException类的完整源代码:


public class IOException extends Exception

{

    static final long serialVersionUID = 7818375828146090155L;


    public IOException()

    {

        super();

    }


    public IOException(String message)

    {

        super(message);

    }


    public IOException(String message, Throwable cause)

    {

        super(message, cause);

    }


    

    public IOException(Throwable cause)

    {

        super(cause);

    }

}



Java异常的注意事项


1、当子类重写父类的带有 throws声明的函数时,其throws声明的异常必须在父类异常的可控范围内——用于处理父类的throws方法的异常处理器,必须也适用于子类的这个带throws方法 。这是为了支持多态。


  例如,父类方法throws 的是2个异常,子类就不能throws 3个及以上的异常。父类throws IOException,子类就必须throws IOException或者IOException的子类。


示例:


class Father

{

    public void start() throws IOException

    {

        throw new IOException();

    }

}


class Son extends Father

{

    public void start() throws Exception

    {

        throw new SQLException();

    }

}

/**********************假设上面的代码是允许的(实质是错误的)***********************/

class Test

{

    public static void main(String[] args)

    {

        Father[] objs = new Father[2];

        objs[0] = new Father();

        objs[1] = new Son();


        for(Father obj:objs)

        {

        //因为Son类抛出的实质是SQLException,而IOException无法处理它。

        //那么这里的try。。catch就不能处理Son中的异常。

        //多态就不能实现了。

            try {

                 obj.start();

            }catch(IOException)

            {

                 //处理IOException

            }

         }

   }

}


  2、Java程序可以是多线程的。每一个线程都是一个独立的执行流,独立的函数调用栈。如果程序只有一个线程,那么没有被任何代码处理的异常 会导致程序终止。如果是多线程的,那么没有被任何代码处理的异常仅仅会导致异常所在的线程结束。


  也就是说,Java中的异常是线程独立的,线程的问题应该由线程自己来解决,而不要委托到外部,也不会直接影响到其它线程的执行。



finally块和return


首先一个不容易理解的事实:在 try块中即便有return,break,continue等改变执行流的语句,finally也会执行。


public static void main(String[] args)

{

    int re = bar();

    System.out.println(re);

}

private static int bar() 

{

    try{

        return 5;

    } finally{

        System.out.println("finally");

    }

}

/*输出:

finally

*/


很多人面对这个问题时,总是在归纳执行的顺序和规律,小编总结了一个方法。用如下GIF图说明。


1568173757953680.gif


  也就是说:try...catch...finally中的return 只要能执行,就都执行了,他们共同向同一个内存地址(假设地址是0x80)写入返回值,后执行的将覆盖先执行的数据,而真正被调用者取的返回值就是最后一次写入的。那么,按照这个思想,下面的这个例子也就不难理解了。


  finally中的return 会覆盖 try 或者catch中的返回值。


public static void main(String[] args)

    {

        int result;

        

        result  =  foo();

        System.out.println(result);     /////////2

        

        result = bar();

        System.out.println(result);    /////////2

    }


    @SuppressWarnings("finally")

    public static int foo()

    {

        trz{

            int a = 5 / 0;

        } catch (Exception e){

            return 1;

        } finally{

            return 2;

        }


    }


    @SuppressWarnings("finally")

    public static int bar()

    {

        try {

            return 1;

        }finally {

            return 2;

        }

    }


finally中的return会抑制(消灭)前面try或者catch块中的异常


class TestException

{

    public static void main(String[] args)

    {

        int result;

        try{

            result = foo();

            System.out.println(result);           //输出100

        } catch (Exception e){

            System.out.println(e.getMessage());    //没有捕获到异常

        }

        

        

        try{

            result  = bar();

            System.out.println(result);           //输出100

        } catch (Exception e){

            System.out.println(e.getMessage());    //没有捕获到异常

        }

    }

    

    //catch中的异常被抑制

    @SuppressWarnings("finally")

    public static int foo() throws Exception

    {

        try {

            int a = 5/0;

            return 1;

        }catch(ArithmeticException amExp) {

            throw new Exception("我将被忽略,因为下面的finally中使用了return");

        }finally {

            return 100;

        }

    }

    

    //try中的异常被抑制

    @SuppressWarnings("finally")

    public static int bar() throws Exception

    {

        try {

            int a = 5/0;

            return 1;

        }finally {

            return 100;

        }

    }

}


finally中的异常会覆盖(消灭)前面try或者catch中的异常


class TestException

{

    public static void main(String[] args)

    {

        int result;

        try{

            result = foo();

        } catch (Exception e){

            System.out.println(e.getMessage());    //输出:我是finaly中的Exception

        }

        

        

        try{

            result  = bar();

        } catch (Exception e){

            System.out.println(e.getMessage());    //输出:我是finaly中的Exception

        }

    }

    

    //catch中的异常被抑制

    @SuppressWarnings("finally")

    public static int foo() throws Exception

    {

        try {

            int a = 5/0;

            return 1;

        }catch(ArithmeticException amExp) {

            throw new Exception("我将被忽略,因为下面的finally中抛出了新的异常");

        }finally {

            throw new Exception("我是finaly中的Exception");

        }

    }

    

    //try中的异常被抑制

    @SuppressWarnings("finally")

    public static int bar() throws Exception

    {

        try {

            int a = 5/0;

            return 1;

        }finally {

            throw new Exception("我是finaly中的Exception");

        }

        

    }

}


注意:


  不要在fianlly中使用return。


  不要在finally中抛出异常。


  减轻finally的任务,不要在finally中做一些其它的事情,finally块仅仅用来释放资源是最合适的。


  将尽量将所有的return写在函数的最后面,而不是try ... catch ... finally中。


以上就是动力节点Java培训机构小编介绍的“JAVA中异常与异常处理详解”的内容,希望对大家有帮助,更多Java最新资讯请继续关注动力节点Java培训机构官网,每天会有精彩内容分享与你。


相关视频教程推荐


java初级入门教程下载——Java自定义异常:http://www.bjpowernode.com/xiazai/1023.html


java初级入门教程下载——异常捕获预处理:http://www.bjpowernode.com/xiazai/2555.html



选你想看

你适合学Java吗?4大专业测评方法

代码逻辑 吸收能力 技术学习能力 综合素质

先测评确定适合在学习

在线申请免费测试名额
价值1998元实验班免费学
姓名
手机
提交