通过解析异常获取方法名称并包含参数 [英] Get the method name and it's contained parameters by parsing the exception

查看:514
本文介绍了通过解析异常获取方法名称并包含参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我收到例如 IOException RunTimeException 时,我只能知道类中的行号。

When I received an exception such as IOException or RunTimeException, I can only know the line number in the class.

我的第一个问题。是否可以通过异常检索方法名称?
第二,是否可以通过行号检索该方法和该方法的参数?

First of my question. Is it possible to retrieve the method name through exception? Second, is it possible to retrieve the method and the parameter of this method by line number?

p.s。我需要知道确切的方法名称及其参数,因为我想区分重载方法。为了区分重载方法,我所知道的是确定其参数。

p.s. I need to know the exact method name and its parameters, because I want to distinguish the overloading methods. To distinguish overloading methods, all that I know is to determine its parameters.

推荐答案

try{
//your code here}
catch(Exception e){
  for (StackTraceElement st : e.getStackTrace())
  {
    System.out.println("Class: " + st.getClassName() + " Method : " 
                      +  st.getMethodName() + " line : " + st.getLineNumber());  
   }
}

如上面的代码所示,你可以获取stackTrace并循环,以获取所有的方法名称和行号,更多信息请参考 http://download.oracle.com/javase/1.4.2/docs/api/java/lang/StackTraceElement.html

as you can see in the code above, you can get the stackTrace and loop over it to get all the method names and line numbers, refer to this for more info http://download.oracle.com/javase/1.4.2/docs/api/java/lang/StackTraceElement.html

这篇关于通过解析异常获取方法名称并包含参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆