Java Exception Rethrowing

来源:互联网 发布:快消品网络商学院 编辑:程序博客网 时间:2024/05/20 04:48

Java Exception Rethrowing

                                                                      ------------- fillInStackTrace ()

 

The fillInStackTrace () method is to fill in the execution stack trace. This method records within this Throwable object information about the current state of the stack frames for the current thread. It will return a reference to a Throwable object, which is useful when rethrow an exception or an exception.

 

For example 1,

public class FillStackTrace {

         public static void f()throws Exception{

                  System.out.println("f() throws an Exception");

                   throw new Exception("From f()");

         }

         public static void g() throws Exception {

                   try{

                            f();

                  }catch(Exception e){

                            System.err.println("cault in g()");

                            e.printStackTrace();

                            throw e;

                   }

         }

        

         public static void main(String[] args) throws Exception{

                   try{

                            g();

                  }catch(Exception e){

                            System.err.println("cault in main");

                            e.printStackTrace();

                   }

         }

}

 

Output:

f() throws an Exception

cault in g()

java.lang.Exception: From f()

    at FillStackTrace.f(FillStackTrace.java:5)

    at FillStackTrace.g(FillStackTrace.java:9)

    at FillStackTrace.main(FillStackTrace.java:22)

cault in main

java.lang.Exception: From f()

    at FillStackTrace.f(FillStackTrace.java:5)

    at FillStackTrace.g(FillStackTrace.java:9)

    at FillStackTrace.main(FillStackTrace.java:22)

 

You can see the out from the line “e.printStackTrace()” in main method:

at FillStackTrace.f(FillStackTrace.java:5)

    at FillStackTrace.g(FillStackTrace.java:9)

    at FillStackTrace.main(FillStackTrace.java:22)

 

The information you print about the exception in printStackTrace () of f() method includes the exception origin, not the place where you rethrow it. Because rethrowing an exception will cause it to go to the exception handlers in the next-higher context. Any further catch in the same try block will be ignored. Everything about the exception object is preserved, so the handler at the higher context that catches the specific exception type can extract all the information from that object.

 

If you want to change the origin of the exception to the place where you rethrow it, use fillInStackTrace (), which returns a Throwable object that it creates by stuffing the current stack information into the old exception object. 

For example 2:

public class FillStackTrace {

         public static void f() throws Exception{

                  System.out.println("f() throws an Exception");

                   throw new Exception("From f()");

         }

         public static void g() throws Throwable {

                   try{

                            f();

                  }catch(Exception e){

                            System.err.println("cault in g()");

                            e.printStackTrace();

                            throw e .fillInStackTrace();

                   }

         }

        

         public static void main(String[] args) throws Throwable{

                   try{

                            g();

                  }catch(Exception e){

                            System.err.println("cault in main");

                            e.printStackTrace();

                   }

         }

}

 

Output:

f() throws an Exception

cault in g()

java.lang.Exception: From f()

    at FillStackTrace.f(FillStackTrace.java:6)

    at FillStackTrace.g(FillStackTrace.java:10)

    at FillStackTrace.main(FillStackTrace.java:23)

cault in main

java.lang.Exception: From f()

    at FillStackTrace.g(FillStackTrace.java:14)

    at FillStackTrace.main(FillStackTrace.java:23)

 

The different between example 1 and example 2 are the rethrowing line and the exception specification of method main and g. Because fillInStackTrace () produces a reference to a Throwable object, since Throwable is the base class of Exception.

 

原创粉丝点击