我的问题有没有办法实现像printStackTrace()
这样的方法
我确实不知道如何开始这样做,这可能吗?
是getStackTrace()
对我来说是一个不错的选择。
最佳答案
这是完成此操作的官方方式( grepcode 格式):
646 private void printStackTrace(PrintStreamOrWriter s) {
647 // Guard against malicious overrides of Throwable.equals by
648 // using a Set with identity equality semantics.
649 Set<Throwable> dejaVu =
650 Collections.newSetFromMap(new IdentityHashMap<Throwable, Boolean>());
651 dejaVu.add(this);
653 synchronized (s.lock()) {
654 // Print our stack trace
655 s.println(this);
656 StackTraceElement[] trace = getOurStackTrace();
657 for (StackTraceElement traceElement : trace)
658 s.println("\tat " + traceElement);
660 // Print suppressed exceptions, if any
661 for (Throwable se : getSuppressed())
662 se.printEnclosedStackTrace(s, trace, SUPPRESSED_CAPTION, "\t", dejaVu);
664 // Print cause, if any
665 Throwable ourCause = getCause();
666 if (ourCause != null)
667 ourCause.printEnclosedStackTrace(s, trace, CAUSE_CAPTION, "", dejaVu);
668 }
669 }
处理的问题比您预期的要多一些,但您可以轻松删除任何您不关心的问题。
关于java - 模仿java中的printStackTrace()方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21704924/