2014-2-13 java 异常 日记

来源:互联网 发布:fast隐藏网络 编辑:程序博客网 时间:2024/06/05 21:01

1.java 异常处理理论上有两种模式:一种是终止模型,无法恢复,无法继续执行,一种是恢复模型。

2.自定义异常类,必须从已有的异常类继承,最好的选择就是相近的异常类继承,建立新的异常类最好的方法就是让编译器产生默认的构造方法如:

class SimpleException extends Exception


这种最实用,对异常来说,最重要的部分就是类名。

 

3 Exception是所有与编程有关的异常类的基类。

 

4. printStackTrace方法所提供的信息都可以通过 e.getStackTrace()方法获得,这个方法将直接返回一个由栈轨迹元素组成的数组,每个元素表示栈中的一帧,元素0是栈顶元素,并且是调用序列中最后一个方法调用,这个Throwable被创建和抛出之处。

 

5  重新抛出异常,在catch块中还可以使用throw 来重新抛出异常,后面的catch块就会被忽略,如果重新抛出后,printStackTrace方法还是显示原来抛出死后的信息,而并非重新抛出的信息,要想更新这个信息,就要使用fillInStackTrace()方法,返回一个Throwable对象。

/** *  */package com.bdr.irts.test;/** * @author Administrator * */public class MyReThrowing{    public static void f() throws Exception    {        System.out.println("这是异常的源头!");        throw new Exception("throw form method f");    }        public static void m1() throws Exception    {        try        {            f();        }        catch (Exception e)        {            // TODO Auto-generated catch block            System.out.println("第一次调用");            e.printStackTrace();            throw e;        }    }        public static void m2() throws Exception    {        try        {            m1();        }        catch (Exception e)        {            // TODO Auto-generated catch block            System.out.println("第二次次调用");            e.printStackTrace();            throw e;            // throw (Exception)e.fillInStackTrace();        }    }        public static void main(String[] args)    {        try        {            m2();        }        catch (Exception e)        {            // TODO Auto-generated catch block            System.out.println("main method");            e.printStackTrace();        }    }}

第一种的输出结果

这是异常的源头!
第一次调用
java.lang.Exception: throw form method f
 at com.bdr.irts.test.MyReThrowing.f(MyReThrowing.java:15)
 at com.bdr.irts.test.MyReThrowing.m1(MyReThrowing.java:22)
 at com.bdr.irts.test.MyReThrowing.m2(MyReThrowing.java:37)
 at com.bdr.irts.test.MyReThrowing.main(MyReThrowing.java:55)
第二次次调用
java.lang.Exception: throw form method f
 at com.bdr.irts.test.MyReThrowing.f(MyReThrowing.java:15)
 at com.bdr.irts.test.MyReThrowing.m1(MyReThrowing.java:22)
 at com.bdr.irts.test.MyReThrowing.m2(MyReThrowing.java:37)
 at com.bdr.irts.test.MyReThrowing.main(MyReThrowing.java:55)
main methodjava.lang.Exception: throw form method f

 at com.bdr.irts.test.MyReThrowing.f(MyReThrowing.java:15)
 at com.bdr.irts.test.MyReThrowing.m1(MyReThrowing.java:22)
 at com.bdr.irts.test.MyReThrowing.m2(MyReThrowing.java:37)
 at com.bdr.irts.test.MyReThrowing.main(MyReThrowing.java:55)

第二种输出结果

这是异常的源头!
第一次调用
java.lang.Exception: throw form method f
 at com.bdr.irts.test.MyReThrowing.f(MyReThrowing.java:15)
 at com.bdr.irts.test.MyReThrowing.m1(MyReThrowing.java:22)
 at com.bdr.irts.test.MyReThrowing.m2(MyReThrowing.java:37)
第二次次调用
main method
 at com.bdr.irts.test.MyReThrowing.main(MyReThrowing.java:55)
java.lang.Exception: throw form method f
 at com.bdr.irts.test.MyReThrowing.f(MyReThrowing.java:15)
 at com.bdr.irts.test.MyReThrowing.m1(MyReThrowing.java:22)
 at com.bdr.irts.test.MyReThrowing.m2(MyReThrowing.java:37)
 at com.bdr.irts.test.MyReThrowing.main(MyReThrowing.java:55)
java.lang.Exception: throw form method f
 at com.bdr.irts.test.MyReThrowing.m2(MyReThrowing.java:47)
 at com.bdr.irts.test.MyReThrowing.main(MyReThrowing.java:55)

 

6  只能在代码中忽略RuntimeException异常和它的子类异常,其他类型的异常都会由编译器强制实施,RuntimeException是编程错误。
 

0 0
原创粉丝点击