2

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

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
laura
  • 155
  • 3
  • 13
  • 2
    You haven't accepted any answers to your questions yet - this may discourage people from helping you. See the [FAQ](http://stackoverflow.com/faq) for details on how to do it. – Péter Török Oct 05 '10 at 13:47
  • Why do you require this to get by method calls ? You could directly hard code it as you know package name, class name and method name. – YoK Oct 05 '10 at 13:49
  • 2
    Have you considered just using a logging framework and logging the exception? Most logging frameworks will print a stacktrace, which is better than just printing the name of the current class and method – NamshubWriter Oct 05 '10 at 14:05

8 Answers8

3
e.printStackTrace();
Arne Deutsch
  • 14,629
  • 5
  • 53
  • 72
2
e.getStackTrace()[0].getMethodName();
jmj
  • 237,923
  • 42
  • 401
  • 438
1

e.printStackTrace(); - that will print the whole exception stracktrace - i.e. all the methods + line numbers.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
1

e.getStackTrace()[0].getMethodName()

kartheek
  • 763
  • 3
  • 7
1

You can use the example below:

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());

      }
   }
}
user85421
  • 28,957
  • 10
  • 64
  • 87
Sekhar
  • 11
  • 1
0

If you throw the exception, is affecting your performance. Is not needed throw the exception,

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());
 }
}  
0

Have ou noticed you already have the information in your exception's stacktrace ?

Indeed, Exception class provides a getStackTrace() method that returns you an array of StacktraceElement. Each of these elements gives you class name, method name, and some other details. What you can do is look in this array if you find one of your three methods, and voila !

However, be warned that detection of method name may fail if you use an obfuscator on your code.

Riduidel
  • 22,052
  • 14
  • 85
  • 185
0

The preferred option.....

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

or (not a wise option since you don't know what exception is thrown an you have no Stack Trace Elements printed).

catch(Exception e) {
    System.out.println(e.getStackTrace()[0].getMethodName());
}
Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
  • I tried it but it doesn't show me the name , in the console I see only "run" I do not know I haven't wrote it and my class and methode doesn't have "run" name ?? – laura Oct 05 '10 at 14:05
  • I tried : catch(Exception e) { System.out.println(e.getStacktrace()[0].getMethodName()); } – laura Oct 05 '10 at 14:09
  • Are there any exception thrown? – Buhake Sindi Oct 05 '10 at 14:20
  • the java tries to access to an object and could't but no message shown with catch(Exception e) { System.out.println(e.getStacktrace()[0].getMethodName()); } – laura Oct 05 '10 at 14:29
  • It does work for me....make sure you typed `e.getStackTrace()[0].getMethodName()` (capital "S" and capital "T") – Buhake Sindi Oct 05 '10 at 14:50
  • I tried the following : System.out.println("failure in file "+e.getStackTrace()[0].getMethodName() ); and in the console I got : failure in file ensureUniqueKey – laura Oct 05 '10 at 15:26
  • It showed your method name called `ensureUniqueKey`. To get file name, do `e.getStackTrace()[0].getFileName()` – Buhake Sindi Oct 05 '10 at 15:32
  • sorry, but ensureUniqueKey isn't the name of my method ! – laura Oct 05 '10 at 16:02
  • my methode name is SelectButton() – laura Oct 05 '10 at 16:03
  • then get the last `getStackTrace()` and see if that is it....otherwise you'll have to iterate until you get to the method...the thing is, the method operation that threw the exception is put on top of the stack element trace and the stack trace goes down the list till the parent that ran it (e.g `public static void main`). – Buhake Sindi Oct 06 '10 at 12:49
  • JIC: try `e.getStacktrace()[1]` – Buhake Sindi Oct 11 '10 at 16:56