java—如何获取引发异常的方法名?

tvz2xvvm  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(394)

很难说出这里要问什么。这个问题模棱两可,含糊不清,不完整,过于宽泛,或者是修辞性的,不能以现在的形式得到合理的回答。有关澄清此问题以便重新打开的帮助,请访问帮助中心。
7年前关门了。
我需要获取引发异常的类名和方法名。我该怎么做?提前谢谢。
我的代码:

private void setErrorDetails(final Throwable cause) {
    for (final StackTraceElement ste : cause.getStackTrace()) {
        System.out.println(ste.getClassName()); 
        System.out.println(ste.getMethodName()); 
    }
}

通过这段代码,我可以得到类名和方法名的列表。除此之外,如何获得引发异常的方法和类名
我也试过这个。

final StackTraceElement[] stackTrace = cause.getStackTrace();
    if ((stackTrace != null) && (stackTrace.length > 1)) {
                    final StackTraceElement stElement = stackTrace[0];

                    this.className = stElement.getClassName();
                    this.methodName = stElement.getMethodName();}

这是打印的堆栈跟踪。

16:01:24,093 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.hibernate.ejb.Ejb3Configuration$Ejb3EntityNotFoundDelegate>>>Ejb3Configuration.java

16:01:24,094 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.hibernate.event.internal.DefaultLoadEventListener>>>DefaultLoadEventListener.java

16:01:24,095 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.hibernate.event.internal.DefaultLoadEventListener>>>DefaultLoadEventListener.java

16:01:24,096 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.hibernate.event.internal.DefaultLoadEventListener>>>DefaultLoadEventListener.java

16:01:24,097 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.hibernate.internal.SessionImpl>>>SessionImpl.java

16:01:24,097 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.hibernate.internal.SessionImpl>>>SessionImpl.java

16:01:24,098 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.hibernate.type.EntityType>>>EntityType.java

16:01:24,099 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.hibernate.type.EntityType>>>EntityType.java

16:01:24,099 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.hibernate.engine.internal.TwoPhaseLoad>>>TwoPhaseLoad.java

16:01:24,100 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.hibernate.loader.Loader>>>Loader.java

16:01:24,101 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.hibernate.loader.Loader>>>Loader.java

16:01:24,101 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.hibernate.loader.Loader>>>Loader.java

16:01:24,102 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.hibernate.loader.Loader>>>Loader.java

16:01:24,103 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.hibernate.loader.Loader>>>Loader.java

16:01:24,103 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.hibernate.loader.Loader>>>Loader.java

16:01:24,104 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.hibernate.loader.Loader>>>Loader.java

16:01:24,104 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.hibernate.loader.hql.QueryLoader>>>QueryLoader.java

16:01:24,105 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.hibernate.hql.internal.ast.QueryTranslatorImpl>>>QueryTranslatorImpl.java

16:01:24,106 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.hibernate.engine.query.spi.HQLQueryPlan>>>HQLQueryPlan.java

16:01:24,107 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.hibernate.internal.SessionImpl>>>SessionImpl.java

16:01:24,107 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.hibernate.internal.QueryImpl>>>QueryImpl.java

16:01:24,108 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.hibernate.ejb.QueryImpl>>>QueryImpl.java

16:01:24,109 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.jboss.as.jpa.container.QueryNonTxInvocationDetacher>>>QueryNonTxInvocationDetacher.java

16:01:24,109 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.cgi.hix.planmanagement.portal.PortalHomeDA>>>PortalHomeDA.java

16:01:24,110 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.cgi.hix.planmanagement.portal.PortalHomeAction>>>PortalHomeAction.java

16:01:24,111 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????sun.reflect.NativeMethodAccessorImpl>>>NativeMethodAccessorImpl.java

16:01:24,112 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????sun.reflect.NativeMethodAccessorImpl>>>NativeMethodAccessorImpl.java

16:01:24,112 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????sun.reflect.DelegatingMethodAccessorImpl>>>DelegatingMethodAccessorImpl.java

16:01:24,113 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????java.lang.reflect.Method>>>Method.java

16:01:24,114 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.DefaultActionInvocation>>>DefaultActionInvocation.java

16:01:24,115 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.DefaultActionInvocation>>>DefaultActionInvocation.java

16:01:24,115 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.DefaultActionInvocation>>>DefaultActionInvocation.java

16:01:24,116 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor>>>ExceptionMappingInterceptor.java

16:01:24,117 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.DefaultActionInvocation>>>DefaultActionInvocation.java

16:01:24,118 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.validator.ValidationInterceptor>>>ValidationInterceptor.java

16:01:24,119 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor>>>AnnotationValidationInterceptor.java

16:01:24,120 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.interceptor.MethodFilterInterceptor>>>MethodFilterInterceptor.java

16:01:24,121 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.DefaultActionInvocation>>>DefaultActionInvocation.java

16:01:24,121 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.softwareforge.struts2.breadcrumb.BreadCrumbInterceptor>>>BreadCrumbInterceptor.java

16:01:24,122 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.DefaultActionInvocation>>>DefaultActionInvocation.java

16:01:24,123 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.cgi.hix.framework.audit.AuditManager>>>AuditManager.java

16:01:24,124 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.DefaultActionInvocation>>>DefaultActionInvocation.java

16:01:24,125 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.cgi.hix.framework.security.SecurityManager>>>SecurityManager.java

16:01:24,126 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.DefaultActionInvocation>>>DefaultActionInvocation.java

16:01:24,126 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.apache.struts2.interceptor.debugging.DebuggingInterceptor>>>DebuggingInterceptor.java

16:01:24,127 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.DefaultActionInvocation>>>DefaultActionInvocation.java

16:01:24,128 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor>>>DefaultWorkflowInterceptor.java

16:01:24,129 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.interceptor.MethodFilterInterceptor>>>MethodFilterInterceptor.java

16:01:24,130 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.DefaultActionInvocation>>>DefaultActionInvocation.java

16:01:24,131 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.validator.ValidationInterceptor>>>ValidationInterceptor.java

16:01:24,132 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor>>>AnnotationValidationInterceptor.java

16:01:24,133 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.interceptor.MethodFilterInterceptor>>>MethodFilterInterceptor.java

16:01:24,133 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.DefaultActionInvocation>>>DefaultActionInvocation.java

16:01:24,134 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor>>>ConversionErrorInterceptor.java

16:01:24,135 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.DefaultActionInvocation>>>DefaultActionInvocation.java

16:01:24,136 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.interceptor.ParametersInterceptor>>>ParametersInterceptor.java

16:01:24,137 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.interceptor.MethodFilterInterceptor>>>MethodFilterInterceptor.java

16:01:24,138 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.DefaultActionInvocation>>>DefaultActionInvocation.java

16:01:24,138 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.interceptor.ParametersInterceptor>>>ParametersInterceptor.java

16:01:24,139 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.interceptor.MethodFilterInterceptor>>>MethodFilterInterceptor.java

16:01:24,140 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.DefaultActionInvocation>>>DefaultActionInvocation.java

16:01:24,141 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.interceptor.StaticParametersInterceptor>>>StaticParametersInterceptor.java

16:01:24,142 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.DefaultActionInvocation>>>DefaultActionInvocation.java

16:01:24,142 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.apache.struts2.interceptor.MultiselectInterceptor>>>MultiselectInterceptor.java

16:01:24,143 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.DefaultActionInvocation>>>DefaultActionInvocation.java

16:01:24,144 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.apache.struts2.interceptor.CheckboxInterceptor>>>CheckboxInterceptor.java

16:01:24,145 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.DefaultActionInvocation>>>DefaultActionInvocation.java

16:01:24,146 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.apache.struts2.interceptor.FileUploadInterceptor>>>FileUploadInterceptor.java

16:01:24,147 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.DefaultActionInvocation>>>DefaultActionInvocation.java

16:01:24,147 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor>>>ModelDrivenInterceptor.java

16:01:24,148 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.DefaultActionInvocation>>>DefaultActionInvocation.java

16:01:24,149 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor>>>ScopedModelDrivenInterceptor.java

16:01:24,150 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.DefaultActionInvocation>>>DefaultActionInvocation.java

16:01:24,151 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.interceptor.ChainingInterceptor>>>ChainingInterceptor.java

16:01:24,151 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.DefaultActionInvocation>>>DefaultActionInvocation.java

16:01:24,152 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.interceptor.PrepareInterceptor>>>PrepareInterceptor.java

16:01:24,153 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.interceptor.MethodFilterInterceptor>>>MethodFilterInterceptor.java

16:01:24,154 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.DefaultActionInvocation>>>DefaultActionInvocation.java

16:01:24,155 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.interceptor.I18nInterceptor>>>I18nInterceptor.java

16:01:24,155 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.DefaultActionInvocation>>>DefaultActionInvocation.java

16:01:24,156 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.apache.struts2.interceptor.ServletConfigInterceptor>>>ServletConfigInterceptor.java

16:01:24,157 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.DefaultActionInvocation>>>DefaultActionInvocation.java

16:01:24,158 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.interceptor.AliasInterceptor>>>AliasInterceptor.java

16:01:24,158 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.DefaultActionInvocation>>>DefaultActionInvocation.java

16:01:24,159 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor>>>ExceptionMappingInterceptor.java

16:01:24,160 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.DefaultActionInvocation>>>DefaultActionInvocation.java

16:01:24,161 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.apache.struts2.impl.StrutsActionProxy>>>StrutsActionProxy.java

16:01:24,162 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.apache.struts2.dispatcher.Dispatcher>>>Dispatcher.java

16:01:24,162 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.apache.struts2.dispatcher.ng.ExecuteOperations>>>ExecuteOperations.java

16:01:24,163 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter>>>StrutsPrepareAndExecuteFilter.java

16:01:24,164 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.apache.catalina.core.ApplicationFilterChain>>>ApplicationFilterChain.java

16:01:24,165 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.apache.catalina.core.ApplicationFilterChain>>>ApplicationFilterChain.java

16:01:24,166 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.apache.catalina.core.StandardWrapperValve>>>StandardWrapperValve.java

16:01:24,167 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.apache.catalina.core.StandardContextValve>>>StandardContextValve.java

16:01:24,167 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve>>>WebNonTxEmCloserValve.java

16:01:24,168 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.jboss.as.web.security.SecurityContextAssociationValve>>>SecurityContextAssociationValve.java

16:01:24,169 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.apache.catalina.core.StandardHostValve>>>StandardHostValve.java

16:01:24,170 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.apache.catalina.valves.ErrorReportValve>>>ErrorReportValve.java

16:01:24,170 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.apache.catalina.core.StandardEngineValve>>>StandardEngineValve.java

16:01:24,171 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.apache.catalina.connector.CoyoteAdapter>>>CoyoteAdapter.java

16:01:24,172 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.apache.coyote.http11.Http11Processor>>>Http11Processor.java

16:01:24,173 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler>>>Http11Protocol.java

16:01:24,173 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.apache.tomcat.util.net.JIoEndpoint$Worker>>>JIoEndpoint.java

16:01:24,174 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????java.lang.Thread>>>Thread.java

我已经打印了堆栈跟踪。我要接电话16:01:24110。

y0u0uwnf

y0u0uwnf1#

本文展示了如何使用logback过滤掉不希望在日志中的堆栈跟踪中看到的包。
同样的原则也应该应用在java代码中,您只需要在类名上构造过滤器。

fzsnzjdm

fzsnzjdm2#

实际引发初始异常的方法是堆栈跟踪的根本原因中的第一个元素:

private void setErrorDetails(final Throwable cause)
{
     Throwable rootCause = cause;
     while(rootCause.getCause() != null &&  rootCause.getCause() != rootCause)
          rootCause = rootCause.getCause();

    System.out.println(rootCause.getStackTrace()[0].getClassName()); 
    System.out.println(rootCause.getStackTrace()[0].getMethodName()); 
    }
}

相关问题