title: How to get the method name from which the exception is thrown?
body: <p>I need to get the class name and the method name from which the exception is thrown. How can I do that? Thanks in advance.</p>
  
  <p>My code:</p>
  
  <pre><code>private void setErrorDetails(final Throwable cause) {
      for (final StackTraceElement ste : cause.getStackTrace()) {
          System.out.println(ste.getClassName()); 
          System.out.println(ste.getMethodName()); 
      }
  }
  </code></pre>
  
  <p>By this code, I am able to get a list of class names and method names.Out of that, how can I get the method and class name which has thrown the exception</p>
  
  <p>I also tried this.</p>
  
  <pre><code> final StackTraceElement[] stackTrace = cause.getStackTrace();
      if ((stackTrace != null) &amp;&amp; (stackTrace.length &gt; 1)) {
                      final StackTraceElement stElement = stackTrace[0];
  
                      this.className = stElement.getClassName();
                      this.methodName = stElement.getMethodName();}
  </code></pre>
  
  <p>This is the printed stacktrace.</p>
  
  <pre><code>16:01:24,093 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.hibernate.ejb.Ejb3Configuration$Ejb3EntityNotFoundDelegate&gt;&gt;&gt;Ejb3Configuration.java
  
  16:01:24,094 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.hibernate.event.internal.DefaultLoadEventListener&gt;&gt;&gt;DefaultLoadEventListener.java
  
  16:01:24,095 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.hibernate.event.internal.DefaultLoadEventListener&gt;&gt;&gt;DefaultLoadEventListener.java
  
  16:01:24,096 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.hibernate.event.internal.DefaultLoadEventListener&gt;&gt;&gt;DefaultLoadEventListener.java
  
  16:01:24,097 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.hibernate.internal.SessionImpl&gt;&gt;&gt;SessionImpl.java
  
  16:01:24,097 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.hibernate.internal.SessionImpl&gt;&gt;&gt;SessionImpl.java
  
  16:01:24,098 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.hibernate.type.EntityType&gt;&gt;&gt;EntityType.java
  
  16:01:24,099 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.hibernate.type.EntityType&gt;&gt;&gt;EntityType.java
  
  16:01:24,099 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.hibernate.engine.internal.TwoPhaseLoad&gt;&gt;&gt;TwoPhaseLoad.java
  
  16:01:24,100 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.hibernate.loader.Loader&gt;&gt;&gt;Loader.java
  
  16:01:24,101 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.hibernate.loader.Loader&gt;&gt;&gt;Loader.java
  
  16:01:24,101 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.hibernate.loader.Loader&gt;&gt;&gt;Loader.java
  
  16:01:24,102 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.hibernate.loader.Loader&gt;&gt;&gt;Loader.java
  
  16:01:24,103 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.hibernate.loader.Loader&gt;&gt;&gt;Loader.java
  
  16:01:24,103 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.hibernate.loader.Loader&gt;&gt;&gt;Loader.java
  
  16:01:24,104 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.hibernate.loader.Loader&gt;&gt;&gt;Loader.java
  
  16:01:24,104 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.hibernate.loader.hql.QueryLoader&gt;&gt;&gt;QueryLoader.java
  
  16:01:24,105 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.hibernate.hql.internal.ast.QueryTranslatorImpl&gt;&gt;&gt;QueryTranslatorImpl.java
  
  16:01:24,106 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.hibernate.engine.query.spi.HQLQueryPlan&gt;&gt;&gt;HQLQueryPlan.java
  
  16:01:24,107 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.hibernate.internal.SessionImpl&gt;&gt;&gt;SessionImpl.java
  
  16:01:24,107 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.hibernate.internal.QueryImpl&gt;&gt;&gt;QueryImpl.java
  
  16:01:24,108 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.hibernate.ejb.QueryImpl&gt;&gt;&gt;QueryImpl.java
  
  16:01:24,109 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.jboss.as.jpa.container.QueryNonTxInvocationDetacher&gt;&gt;&gt;QueryNonTxInvocationDetacher.java
  
  16:01:24,109 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.cgi.hix.planmanagement.portal.PortalHomeDA&gt;&gt;&gt;PortalHomeDA.java
  
  16:01:24,110 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.cgi.hix.planmanagement.portal.PortalHomeAction&gt;&gt;&gt;PortalHomeAction.java
  
  16:01:24,111 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????sun.reflect.NativeMethodAccessorImpl&gt;&gt;&gt;NativeMethodAccessorImpl.java
  
  16:01:24,112 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????sun.reflect.NativeMethodAccessorImpl&gt;&gt;&gt;NativeMethodAccessorImpl.java
  
  16:01:24,112 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????sun.reflect.DelegatingMethodAccessorImpl&gt;&gt;&gt;DelegatingMethodAccessorImpl.java
  
  16:01:24,113 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????java.lang.reflect.Method&gt;&gt;&gt;Method.java
  
  16:01:24,114 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.DefaultActionInvocation&gt;&gt;&gt;DefaultActionInvocation.java
  
  16:01:24,115 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.DefaultActionInvocation&gt;&gt;&gt;DefaultActionInvocation.java
  
  16:01:24,115 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.DefaultActionInvocation&gt;&gt;&gt;DefaultActionInvocation.java
  
  16:01:24,116 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor&gt;&gt;&gt;ExceptionMappingInterceptor.java
  
  16:01:24,117 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.DefaultActionInvocation&gt;&gt;&gt;DefaultActionInvocation.java
  
  16:01:24,118 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.validator.ValidationInterceptor&gt;&gt;&gt;ValidationInterceptor.java
  
  16:01:24,119 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor&gt;&gt;&gt;AnnotationValidationInterceptor.java
  
  16:01:24,120 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.interceptor.MethodFilterInterceptor&gt;&gt;&gt;MethodFilterInterceptor.java
  
  16:01:24,121 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.DefaultActionInvocation&gt;&gt;&gt;DefaultActionInvocation.java
  
  16:01:24,121 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.softwareforge.struts2.breadcrumb.BreadCrumbInterceptor&gt;&gt;&gt;BreadCrumbInterceptor.java
  
  16:01:24,122 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.DefaultActionInvocation&gt;&gt;&gt;DefaultActionInvocation.java
  
  16:01:24,123 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.cgi.hix.framework.audit.AuditManager&gt;&gt;&gt;AuditManager.java
  
  16:01:24,124 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.DefaultActionInvocation&gt;&gt;&gt;DefaultActionInvocation.java
  
  16:01:24,125 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.cgi.hix.framework.security.SecurityManager&gt;&gt;&gt;SecurityManager.java
  
  16:01:24,126 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.DefaultActionInvocation&gt;&gt;&gt;DefaultActionInvocation.java
  
  16:01:24,126 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.apache.struts2.interceptor.debugging.DebuggingInterceptor&gt;&gt;&gt;DebuggingInterceptor.java
  
  16:01:24,127 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.DefaultActionInvocation&gt;&gt;&gt;DefaultActionInvocation.java
  
  16:01:24,128 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor&gt;&gt;&gt;DefaultWorkflowInterceptor.java
  
  16:01:24,129 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.interceptor.MethodFilterInterceptor&gt;&gt;&gt;MethodFilterInterceptor.java
  
  16:01:24,130 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.DefaultActionInvocation&gt;&gt;&gt;DefaultActionInvocation.java
  
  16:01:24,131 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.validator.ValidationInterceptor&gt;&gt;&gt;ValidationInterceptor.java
  
  16:01:24,132 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor&gt;&gt;&gt;AnnotationValidationInterceptor.java
  
  16:01:24,133 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.interceptor.MethodFilterInterceptor&gt;&gt;&gt;MethodFilterInterceptor.java
  
  16:01:24,133 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.DefaultActionInvocation&gt;&gt;&gt;DefaultActionInvocation.java
  
  16:01:24,134 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor&gt;&gt;&gt;ConversionErrorInterceptor.java
  
  16:01:24,135 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.DefaultActionInvocation&gt;&gt;&gt;DefaultActionInvocation.java
  
  16:01:24,136 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.interceptor.ParametersInterceptor&gt;&gt;&gt;ParametersInterceptor.java
  
  16:01:24,137 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.interceptor.MethodFilterInterceptor&gt;&gt;&gt;MethodFilterInterceptor.java
  
  16:01:24,138 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.DefaultActionInvocation&gt;&gt;&gt;DefaultActionInvocation.java
  
  16:01:24,138 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.interceptor.ParametersInterceptor&gt;&gt;&gt;ParametersInterceptor.java
  
  16:01:24,139 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.interceptor.MethodFilterInterceptor&gt;&gt;&gt;MethodFilterInterceptor.java
  
  16:01:24,140 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.DefaultActionInvocation&gt;&gt;&gt;DefaultActionInvocation.java
  
  16:01:24,141 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.interceptor.StaticParametersInterceptor&gt;&gt;&gt;StaticParametersInterceptor.java
  
  16:01:24,142 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.DefaultActionInvocation&gt;&gt;&gt;DefaultActionInvocation.java
  
  16:01:24,142 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.apache.struts2.interceptor.MultiselectInterceptor&gt;&gt;&gt;MultiselectInterceptor.java
  
  16:01:24,143 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.DefaultActionInvocation&gt;&gt;&gt;DefaultActionInvocation.java
  
  16:01:24,144 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.apache.struts2.interceptor.CheckboxInterceptor&gt;&gt;&gt;CheckboxInterceptor.java
  
  16:01:24,145 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.DefaultActionInvocation&gt;&gt;&gt;DefaultActionInvocation.java
  
  16:01:24,146 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.apache.struts2.interceptor.FileUploadInterceptor&gt;&gt;&gt;FileUploadInterceptor.java
  
  16:01:24,147 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.DefaultActionInvocation&gt;&gt;&gt;DefaultActionInvocation.java
  
  16:01:24,147 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor&gt;&gt;&gt;ModelDrivenInterceptor.java
  
  16:01:24,148 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.DefaultActionInvocation&gt;&gt;&gt;DefaultActionInvocation.java
  
  16:01:24,149 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor&gt;&gt;&gt;ScopedModelDrivenInterceptor.java
  
  16:01:24,150 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.DefaultActionInvocation&gt;&gt;&gt;DefaultActionInvocation.java
  
  16:01:24,151 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.interceptor.ChainingInterceptor&gt;&gt;&gt;ChainingInterceptor.java
  
  16:01:24,151 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.DefaultActionInvocation&gt;&gt;&gt;DefaultActionInvocation.java
  
  16:01:24,152 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.interceptor.PrepareInterceptor&gt;&gt;&gt;PrepareInterceptor.java
  
  16:01:24,153 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.interceptor.MethodFilterInterceptor&gt;&gt;&gt;MethodFilterInterceptor.java
  
  16:01:24,154 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.DefaultActionInvocation&gt;&gt;&gt;DefaultActionInvocation.java
  
  16:01:24,155 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.interceptor.I18nInterceptor&gt;&gt;&gt;I18nInterceptor.java
  
  16:01:24,155 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.DefaultActionInvocation&gt;&gt;&gt;DefaultActionInvocation.java
  
  16:01:24,156 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.apache.struts2.interceptor.ServletConfigInterceptor&gt;&gt;&gt;ServletConfigInterceptor.java
  
  16:01:24,157 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.DefaultActionInvocation&gt;&gt;&gt;DefaultActionInvocation.java
  
  16:01:24,158 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.interceptor.AliasInterceptor&gt;&gt;&gt;AliasInterceptor.java
  
  16:01:24,158 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.DefaultActionInvocation&gt;&gt;&gt;DefaultActionInvocation.java
  
  16:01:24,159 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor&gt;&gt;&gt;ExceptionMappingInterceptor.java
  
  16:01:24,160 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????com.opensymphony.xwork2.DefaultActionInvocation&gt;&gt;&gt;DefaultActionInvocation.java
  
  16:01:24,161 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.apache.struts2.impl.StrutsActionProxy&gt;&gt;&gt;StrutsActionProxy.java
  
  16:01:24,162 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.apache.struts2.dispatcher.Dispatcher&gt;&gt;&gt;Dispatcher.java
  
  16:01:24,162 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.apache.struts2.dispatcher.ng.ExecuteOperations&gt;&gt;&gt;ExecuteOperations.java
  
  16:01:24,163 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter&gt;&gt;&gt;StrutsPrepareAndExecuteFilter.java
  
  16:01:24,164 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.apache.catalina.core.ApplicationFilterChain&gt;&gt;&gt;ApplicationFilterChain.java
  
  16:01:24,165 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.apache.catalina.core.ApplicationFilterChain&gt;&gt;&gt;ApplicationFilterChain.java
  
  16:01:24,166 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.apache.catalina.core.StandardWrapperValve&gt;&gt;&gt;StandardWrapperValve.java
  
  16:01:24,167 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.apache.catalina.core.StandardContextValve&gt;&gt;&gt;StandardContextValve.java
  
  16:01:24,167 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve&gt;&gt;&gt;WebNonTxEmCloserValve.java
  
  16:01:24,168 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.jboss.as.web.security.SecurityContextAssociationValve&gt;&gt;&gt;SecurityContextAssociationValve.java
  
  16:01:24,169 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.apache.catalina.core.StandardHostValve&gt;&gt;&gt;StandardHostValve.java
  
  16:01:24,170 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.apache.catalina.valves.ErrorReportValve&gt;&gt;&gt;ErrorReportValve.java
  
  16:01:24,170 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.apache.catalina.core.StandardEngineValve&gt;&gt;&gt;StandardEngineValve.java
  
  16:01:24,171 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.apache.catalina.connector.CoyoteAdapter&gt;&gt;&gt;CoyoteAdapter.java
  
  16:01:24,172 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.apache.coyote.http11.Http11Processor&gt;&gt;&gt;Http11Processor.java
  
  16:01:24,173 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler&gt;&gt;&gt;Http11Protocol.java
  
  16:01:24,173 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????org.apache.tomcat.util.net.JIoEndpoint$Worker&gt;&gt;&gt;JIoEndpoint.java
  
  16:01:24,174 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) ????????????????????java.lang.Thread&gt;&gt;&gt;Thread.java
  </code></pre>
  
  <p>I have printed the stack trace. And I need to get the line 16:01:24,110.</p>