Вызов метода fillInStackTrace после вызова метода printStackTrace - PullRequest
0 голосов
/ 29 ноября 2011

Blow - это пример, скопированный с Think in Java 4 edition

   public class Rethrowing {
        public static void f() throws Exception {
            System.out.println("originating the exception in f()");
            throw new Exception("thrown from f()");
        }

        public static void h() throws Exception {
            try {
                f();
            } catch (Exception e) {
                System.out.println("Inside h(),e.printStackTrace()");
                e.printStackTrace(System.out); //first print line           throw (Exception) e.fillInStackTrace();
            }
        }

        public static void main(String[] args) {
            try {
                h();
            } catch (Exception e) {
                System.out.println("main: printStackTrace()");
                e.printStackTrace(System.out);
            }
        }
    }


Output:
originating the exception in f()
Inside h(),e.printStackTrace()
java.lang.Exception: thrown from f()
    at Rethrowing.f(Rethrowing.java:20)
    at Rethrowing.h(Rethrowing.java:25)
    at Rethrowing.main(Rethrowing.java:35)
main: printStackTrace()
java.lang.Exception: thrown from f()
    at Rethrowing.f(Rethrowing.java:20)
    at Rethrowing.h(Rethrowing.java:25)
    at Rethrowing.main(Rethrowing.java:35)

When comment //first print line

Output:
originating the exception in f()
Inside h(),e.printStackTrace()
main: printStackTrace()
java.lang.Exception: thrown from f()
    at Rethrowing.h(Rethrowing.java:29)
    at Rethrowing.main(Rethrowing.java:35)

У меня вопрос, почему я сначала вызываю метод e.printStackTrace (printOut out) перед методом fillInStackTrace, а затем, кажется, fillInStackTrace недоступен. Любой может сделать меня лихорадкой, спасибо заранее.

1 Ответ

0 голосов
/ 29 ноября 2011

user917879, при вызове e.fillInStackTrace(); сбрасывается StackTrace. Таким образом, чтобы напечатать текущий StackTrace - перед его сбросом - вам необходимо сначала вызвать e.printStackTrace (printOut out).

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...