English 中文(简体)
• 如何在目前类别中找到方法名称
原标题:how to get the name of method in current class
  • 时间:2010-10-05 13:42:14
  •  标签:
  • java

Hello in my java class Toto, I have 3 static methods I would like to know when I m in one of these methods , how to get and display the name of package.class.methode in the try catch bloc ? I tried in methodeA:

public static void methodeA(){

try{
system.out.println("I do something");

}
catch(Exception e){
system.out.println("failed" +e.getClass().getMethods().toString());
}

but it is not working, how also could I show it in try ? thanks

最佳回答

<代码>e.StackTrace();——这将印刷整个例外轨道——即所有方法+线编号。

问题回答
e.printStackTrace();
e.getStackTrace()[0].getMethodName();

e.getStackTrace(0)。

您可以举以下例子:

public class A 
{
    public static void main(String args[])
    {
        new A().intermediate();   
    }
    void intermediate() 
    {
        new A().exceptionGenerator();
    }
    void exceptionGenerator()
    {
      try
      {
         throw new Exception("Stack which list all calling methods and classes");
      }
      catch( Exception e )
      {
         System.out.println( "Class is :" + e.getStackTrace()[1].getClassName() + 
                             "Method is :" + e.getStackTrace()[1].getMethodName());

         System.out.println("Second level caller details:");
         System.out.println( "Class is :" + e.getStackTrace()[2].getClassName() + 
                             "Method is :" + e.getStackTrace()[2].getMethodName());

      }
   }
}

是否注意到你作为例外已经掌握了信息?

事实上,特殊类别提供了getStackTrace(rel=“nofollow”>StacktraceElement。 每一项内容都给你分类名称、方法名称和其他细节。 如果你找到你的三种方法之一,你可以做些什么?

然而,必须警告的是,如果你在你的法典上使用混淆器,发现方法名称可能失败。

优先选择......。

catch(Exception e) {
    e.printStackTrace();
}

或(不是明智的选择,因为你不知道有人把什么例外抛在了你身上,没有印有痕迹。)

catch(Exception e) {
    System.out.println(e.getStackTrace()[0].getMethodName());
}

如果你放弃这一例外,就会影响你的业绩。 不需要放弃例外,

public class AppMain {

 public static void main(String[] args) {
   new AppMain().execute();
 }

 private void execute(){
   RuntimeException exception = new RuntimeException();
   StackTraceElement currentElement = exception.getStackTrace()[0];

   System.out.println(currentElement.getClassName());
   System.out.println(currentElement.getMethodName());
 }
}  




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签