更新:您最好使用您发现的IDE以外的断点。
否则,您可以通过System.setOut(..)
设置自定义PrintStream
还有,每当印刷的东西,还打印Thread.currentThread().getStackTrace()
理想情况下你应该PrintStream
只是包装原System.out
和分派给它。喜欢的东西:
public class PrintStreamWrapper extends PrintStream {
public PrintStreamWrapper(OutputStream out) {
super(out);
}
public void println(String x) {
StackTraceElement[] trace = Thread.currentThread().getStackTrace();
// print everything, but not to the System.out,
// because you'll end up in a loop.
super.println(x);
}
}
,然后在你的程序的开头:
System.setOut(new PrintStreamWrapper(System.out));
哇!非常感谢!作为另一种选择,我才发现,原来Eclipse是能够打破执行时,抛出一个异常:http://agile.csc.ncsu.edu/SEMaterials/tutorials/eclipse-debugger/#section5_0 – zakmck 2011-05-30 17:03:59
@ user529286是的,这也是一个选项。也许是一个更好的。 – Bozho 2011-05-30 17:06:15
@Bozho:严格地说,包装类应该使用组合而不是继承,正确吗? – 2016-04-15 18:36:39