简体   繁体   English

为什么不打印堆栈跟踪?

[英]Why doesn't the stack trace get printed?

While trying to print the stack trace in my browser window, I fail. 尝试在浏览器窗口中打印stack trace时,我失败了。 Following is the sample of the exception block. 以下是异常块的示例。

try {
    // Add Code Here
} catch(Exception exc) { exc.printStackTrace();}

I am using google app engine as my server. 我正在使用google app engine作为服务器。 What could be the reason the stack trace is not printing ? 堆栈跟踪不打印的原因可能是什么?

Because there is no Exception to catch: 因为没有Exception可以捕获:

try {
    // Add Code Here
}

You will only print a stack trace an Exception is thrown within the try block like this: 您将只打印堆栈跟踪的Exception是中抛出try这样的块:

try {
    throw new RuntimeException();
}

The method in question is implemented like this: 有问题的方法是这样实现的:

public void printStackTrace() {
    printStackTrace(System.err);
}

If your container/IDE/framework redirects the stream then you will not observe it at the expected location, see also System.setErr(...); 如果您的容器/ IDE /框架重定向了流,那么您将不会在预期的位置看到它,另请参见System.setErr(...); . And of course, I expect you to have checked that your code really throws an Exception (if an Error then it will not be caught by your clause). 当然,我希望您已经检查过您的代码是否确实引发了Exception (如果发生Error则子句不会捕获该Exception )。

You will need to change the default error output stream. 您将需要更改默认错误输出流。 Since you are most probably coding a servlet , you could change to : 由于您很可能正在编写servlet ,因此可以更改为:

exc.printStackTrace(response.getWriter());

This will print the exception trace into the browser window. 这会将异常跟踪打印到浏览器窗口中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM