通过解析异常来获取方法名称及其包含的参数

Cha*_* Wu 2 java exception-handling

当我收到诸如IOExceptionRunTimeException之类的异常时,我只能知道班级中的行号.

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

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

Amj*_*sad 6

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

正如您在上面的代码中看到的,您可以获取stackTrace并在其上循环以获取所有方法名称和行号,有关详细信息,请参阅此http://download.oracle.com/javase/1.4.2/文档/ API/JAVA/LANG/StackTraceElement.html