如何在java中打印完整的堆栈跟踪

时间:2010-06-03 22:53:17

标签: java

我想阅读我捕获的异常的完整堆栈跟踪。

例如:

org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot load JDBC driver class 'com.ibm.db2.jcc.DB2Driver'
 at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1136)
 at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:880)
 at com.azurian.lce.usuarios.ConnectionManager.getConnection(ConnectionManager.java:65)
 at com.azurian.lce.usuarios.db2.UsuarioDAOImpl.autenticar(UsuarioDAOImpl.java:101)
 at com.azurian.lce.usuarios.UsuarioServiceImpl.autenticar(UsuarioServiceImpl.java:31)
 at com.azurian.lce.web.admin.actions.LoginAction.execute(LoginAction.java:49)
 at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
 at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
 at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
 at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
 at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
 at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
 at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
 at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: COM.ibm.db2.jcc.DB2Driver
 at java.net.URLClassLoader$1.run(Unknown Source)
 at java.security.AccessController.doPrivileged(Native Method)
 at java.net.URLClassLoader.findClass(Unknown Source)
 at java.lang.ClassLoader.loadClass(Unknown Source)
 at java.lang.ClassLoader.loadClass(Unknown Source)
 at java.lang.ClassLoader.loadClassInternal(Unknown Source)
 at java.lang.Class.forName0(Native Method)
 at java.lang.Class.forName(Unknown Source)
 at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1130)
 ... 23 more

我想阅读“...... 23以上”以查看异常的来源。

4 个答案:

答案 0 :(得分:34)

答案很简单,那些行已经在堆栈跟踪中了:)

 at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:880)
 at com.azurian.lce.usuarios.ConnectionManager.getConnection(ConnectionManager.java:65)
 at com.azurian.lce.usuarios.db2.UsuarioDAOImpl.autenticar(UsuarioDAOImpl.java:101)
 at com.azurian.lce.usuarios.UsuarioServiceImpl.autenticar(UsuarioServiceImpl.java:31)
 at com.azurian.lce.web.admin.actions.LoginAction.execute(LoginAction.java:49)
 at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
 at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
 at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
 at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
 at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
 at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
 at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
 at java.lang.Thread.run(Unknown Source)

基本上,BasicDataSource#createDataSource()发生了以下情况:

try {
    Class.forName(driverClassName); // Line 1130
} catch (ClassNotFoundException e) {
    throw new SQLNestedException(e, "Cannot load JDBC driver class '" + driverClassName + "'"); // Line 1136
}

答案 1 :(得分:31)

BalusC是对的。见这里:http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Throwable.html#printStackTrace()

特别是:

  

注意包含的行的存在   那些角色 ”...”。这些线   表明剩下的   此异常的堆栈跟踪匹配   指示的帧数   堆栈底部的痕迹   由此引起的异常   异常(“封闭”例外)。   这种速记可以大大减少   常见的输出长度   包装异常的情况   从与...相同的方法抛出   “致使例外”被抓住了。

在您的示例中,这意味着:

BasicDataSource.java第1136行抓住了第1130行引发的ClassNotFoundException并将其重新标记为SQLNestedException。因此,ClassNotFoundException的堆栈跟踪的其余部分与上面的SQLNestedException匹配,并且堆栈跟踪以这种更简洁的格式打印。

答案 2 :(得分:11)

当外部异常(SQLNestedException)包装内部异常(ClassNotFoundError)时,它们位于同一个线程上,因此共享一个公共基础到它们的堆栈跟踪。

(23以上......)显示了内部异常的公共堆栈的开始位置,也就是外部异常被抛出的位置。所以,每当你看到(XX更多...)时,只需查看上面的异常就可以看到堆栈跟踪的其余部分。

如果要以编程方式打印堆栈跟踪而不使用常用跟踪的省略号,则可以使用Throwable.getStackTrace()并自行打印出所有元素。

答案 3 :(得分:5)

试一试。这个逻辑循环遍历主要异常及其所有原因,直到没有更多原因(cause == null)留下来处理。这样你可以避免另外23条消息。我还没有测试过这个,但我相信这应该适合你。

BTW - logWriter是一个缓冲的编写器。您可能希望使用System.out.print或任何其他日志记录API。

public static void debugError(final String message, final Throwable th) {
    final String logMessage = "[ERROR] - " + message;

    try {
        logWriter.write(logMessage);
        logWriter.newLine();

        // dump exception stack if specified
        if (null != th) {
            final StackTraceElement[] traces = th.getStackTrace();
            if (null != traces && traces.length > 0) {
                logWriter.write(th.getClass() + ": " + th.getMessage());
                logWriter.newLine();

                for (final StackTraceElement trace : traces) {
                    logWriter.write("    at " + trace.getClassName() + '.' + trace.getMethodName() + '(' + trace.getFileName() + ':' + trace.getLineNumber() + ')');
                    logWriter.newLine();
                }
            }

            Throwable cause = th.getCause();
            while (null != cause) {
                final StackTraceElement[] causeTraces = cause.getStackTrace();
                if (null != causeTraces && causeTraces.length > 0) {
                    logWriter.write("Caused By:");
                    logWriter.newLine();
                    logWriter.write(cause.getClass() + ": " + cause.getMessage());
                    logWriter.newLine();

                    for (final StackTraceElement causeTrace : causeTraces) {
                        logWriter.write("    at " + causeTrace.getClassName() + '.' + causeTrace.getMethodName() + '(' + causeTrace.getFileName() + ':' + causeTrace.getLineNumber() + ')');
                        logWriter.newLine();
                    }
                }

                // fetch next cause
                cause = cause.getCause();
            }
        }
    } catch (final IOException ex) {
        System.err.println(logMessage);

        if (null != th) {
            th.printStackTrace();
        }
    }
}