Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Something like Environment.StackTrace in .Net. BTW, Thread.dumpStack() is not what I want - I want to get the stacktrace back, not print it out.
How do I determine if a Scala module is opened as script or if it is regularly imported? This question is about the same issue as previous Python question: how do I determine whether a python script is imported as module or run as script? but for Scala
This time I want to get the annotation from the thrown exception. My example code is like this: public class P { @MyAnnotation(stringValue = "FirstType", intValue = 999) public void test() throws Exception { // ... throw new NullPointerException(); } @MyAnnotation(stringValue = "SecondType", intValue = 111) public void test(int a) throws Exception { ...
In C/C++ the filename is returned by FILE and line number is returned by LINE. Java does have a getFileName(), but does not seem to have a corresponding getLineNumber(). It would be nice to be able to do something like this: catch (Exception e) { System.err.println(this.getFileName() + this.getLineNumber() + e.getMessage()); } Is there a way to get the java file/line number?
Let's say there are three consecutive function calls in one try block and all of them throw the same type of exception. How can i figure out which function call threw the caught exception when handling it?
When getting a stacktrace as error report from an application that is already deployed, it would be helpful to also get the actual variable values to reconstruct the system's state at the point before the exception was thrown. Is anything like that feasible in Java and how could one do that? Cheers, Max
Possible Duplicate: In Java, how do i find the caller of a method using stacktrace or reflection? I'm just curious. I was requesting that feature sometimes, but then I solved it with more code. (the calling class said its name while calling the method)
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 ...
I have a static Java method, where I want to know who is its caller. Is it possible to get this information in Java?
I have a generic function that prints exceptions (using log4j): private void _showErrorMessage(Exception e) { log.error(e.getClass() + ": " + e.getMessage() + ": " + e.getCause() + "\n" + e.getStackTrace().toString()); } Instead of seeing the stack trace I'm seeing: [Ljava.lang.StackTraceElement;@49af7e68 How can I view the stack trace of the exception properly? update log.error(e...
My principal problem is how to find out whitch object type called specific method. Is out there any solution that do no use stack trace ? If not why such information are not avaiable ? it could be very helpfull.
Give a simple reference to a Throwable object. I want to just find out what the original class where this was thrown. I no longer am inside this class so all I have is a Throwable object that was passed in from somewhere.
When I received an exception such as IOException or RunTimeException, I can only know the line number the 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. I need to know the exactly about the method name and its parameters, because I want to disting...
I am coding an Error Log Function.So I need to log some information like operator,operate_time,error_message and api_name which CLASS or PROCEDURE the Exception be threw. My problem is that I don't know how to get the api_name in java. A part stacktrace of exception is(I hide name of my company): com.xxxx.commons.database.DbException: {dbFlag: 101, dbMessage: ORA-01461} at com.xxxx.commons...
For debugging purposes, I need to follow the execution of some piece of code, within a class. I would like to generate a log for all method calls, in XML, like : <call class='pack.age.MyClass' method='myMethod1'> <param name='param1'>param1.toString() value</param> ... <call>Call to other method within myMethod1; you get the idea</call> </call> ...
  /*
   * Copyright 2000-2004 Sun Microsystems, Inc.  All Rights Reserved.
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   *
   * This code is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 only, as
   * published by the Free Software Foundation.  Sun designates this
   * particular file as subject to the "Classpath" exception as provided
   * by Sun in the LICENSE file that accompanied this code.
  *
  * This code is distributed in the hope that it will be useful, but WITHOUT
  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  * version 2 for more details (a copy is included in the LICENSE file that
  * accompanied this code).
  *
  * You should have received a copy of the GNU General Public License version
  * 2 along with this work; if not, write to the Free Software Foundation,
  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  *
  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  * CA 95054 USA or visit www.sun.com if you need additional information or
  * have any questions.
  */
 
 package java.lang;

An element in a stack trace, as returned by Throwable.getStackTrace(). Each element represents a single stack frame. All stack frames except for the one at the top of the stack represent a method invocation. The frame at the top of the stack represents the execution point at which the stack trace was generated. Typically, this is the point at which the throwable corresponding to the stack trace was created.

Author(s):
Josh Bloch
Since:
1.4
 
 public final class StackTraceElement implements java.io.Serializable {
     // Normally initialized by VM (public constructor added in 1.5)
     private String declaringClass;
     private String methodName;
     private String fileName;
     private int    lineNumber;

    
Creates a stack trace element representing the specified execution point.

Parameters:
declaringClass the fully qualified name of the class containing the execution point represented by the stack trace element
methodName the name of the method containing the execution point represented by the stack trace element
fileName the name of the file containing the execution point represented by the stack trace element, or null if this information is unavailable
lineNumber the line number of the source line containing the execution point represented by this stack trace element, or a negative number if this information is unavailable. A value of -2 indicates that the method containing the execution point is a native method
Throws:
NullPointerException if declaringClass or methodName is null
Since:
1.5
 
     public StackTraceElement(String declaringClassString methodName,
                              String fileNameint lineNumber) {
         if (declaringClass == null)
             throw new NullPointerException("Declaring class is null");
         if (methodName == null)
             throw new NullPointerException("Method name is null");
 
         this. = declaringClass;
         this.     = methodName;
         this.       = fileName;
         this.     = lineNumber;
     }

    
Returns the name of the source file containing the execution point represented by this stack trace element. Generally, this corresponds to the SourceFile attribute of the relevant class file (as per The Java Virtual Machine Specification, Section 4.7.7). In some systems, the name may refer to some source code unit other than a file, such as an entry in source repository.

Returns:
the name of the file containing the execution point represented by this stack trace element, or null if this information is unavailable.
 
     public String getFileName() {
         return ;
     }

    
Returns the line number of the source line containing the execution point represented by this stack trace element. Generally, this is derived from the LineNumberTable attribute of the relevant class file (as per The Java Virtual Machine Specification, Section 4.7.8).

Returns:
the line number of the source line containing the execution point represented by this stack trace element, or a negative number if this information is unavailable.
    public int getLineNumber() {
        return ;
    }

    
Returns the fully qualified name of the class containing the execution point represented by this stack trace element.

Returns:
the fully qualified name of the Class containing the execution point represented by this stack trace element.
    public String getClassName() {
        return ;
    }

    
Returns the name of the method containing the execution point represented by this stack trace element. If the execution point is contained in an instance or class initializer, this method will return the appropriate special method name, <init> or <clinit>, as per Section 3.9 of The Java Virtual Machine Specification.

Returns:
the name of the method containing the execution point represented by this stack trace element.
    public String getMethodName() {
        return ;
    }

    
Returns true if the method containing the execution point represented by this stack trace element is a native method.

Returns:
true if the method containing the execution point represented by this stack trace element is a native method.
    public boolean isNativeMethod() {
        return  == -2;
    }

    
Returns a string representation of this stack trace element. The format of this string depends on the implementation, but the following examples may be regarded as typical:
  • "MyClass.mash(MyClass.java:9)" - Here, "MyClass" is the fully-qualified name of the class containing the execution point represented by this stack trace element, "mash" is the name of the method containing the execution point, "MyClass.java" is the source file containing the execution point, and "9" is the line number of the source line containing the execution point.
  • "MyClass.mash(MyClass.java)" - As above, but the line number is unavailable.
  • "MyClass.mash(Unknown Source)" - As above, but neither the file name nor the line number are available.
  • "MyClass.mash(Native Method)" - As above, but neither the file name nor the line number are available, and the method containing the execution point is known to be a native method.

    public String toString() {
        return getClassName() + "." +  +
            (isNativeMethod() ? "(Native Method)" :
             ( != null &&  >= 0 ?
              "(" +  + ":" +  + ")" :
              ( != null ?  "("++")" : "(Unknown Source)")));
    }

    
Returns true if the specified object is another StackTraceElement instance representing the same execution point as this instance. Two stack trace elements a and b are equal if and only if:
     equals(a.getFileName(), b.getFileName()) &&
     a.getLineNumber() == b.getLineNumber()) &&
     equals(a.getClassName(), b.getClassName()) &&
     equals(a.getMethodName(), b.getMethodName())
 
where equals is defined as:
     static boolean equals(Object a, Object b) {
         return a==b || (a != null && a.equals(b));
     }
 

Parameters:
obj the object to be compared with this stack trace element.
Returns:
true if the specified object is another StackTraceElement instance representing the same execution point as this instance.
    public boolean equals(Object obj) {
        if (obj==this)
            return true;
        if (!(obj instanceof StackTraceElement))
            return false;
        StackTraceElement e = (StackTraceElement)obj;
        return e.declaringClass.equals() && e.lineNumber == 
            && eq(e.methodName) && eq(e.fileName);
    }
    private static boolean eq(Object aObject b) {
        return a==b || (a != null && a.equals(b));
    }

    
Returns a hash code value for this stack trace element.
    public int hashCode() {
        int result = 31*.hashCode() + .hashCode();
        result = 31*result + ( == null ?   0 : .hashCode());
        result = 31*result + ;
        return result;
    }
    private static final long serialVersionUID = 6992337162326171013L;
New to GrepCode? Check out our FAQ X