Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Easiest way to convert the result of Throwable.getStackTrace() to a string that depicts the stacktrace?
Lots of times in Java logs I'll get something like: Caused by: java.sql.BatchUpdateException: failed batch at org.hsqldb.jdbc.jdbcStatement.executeBatch(jdbcStatement.java:1102) at org.hsqldb.jdbc.jdbcPreparedStatement.executeBatch(jdbcPreparedStatement.java:514) at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48) at org.hibernate.jdbc.AbstractBatcher....
I've had instances of our Java code catch a NullPointerException, but when I try to log the StackTrace (which basically ends up calling Throwable.printStackTrace() ), all I get is: java.lang.NullPointerException Has anyone else come across this? I tried googling for "java null pointer empty stack trace" but didn't come across anything like this.
Which features of JDK 7 (excluding invokedynamic because it is not used by java) causing a new class file version which is not compliant with JDK 6. It seams that all features could be implemented by compiler generating glue codes. For example String in switch statement could be implemented using repeated ifeq statements generated by the compiler. I want to able to give -source 1.7 -target 1.6 ...
Do you know how expensive exception throwing and handling in java is? We had several discussions about the real cost of exceptions in our team. Some avoid them as often as possible, some say the loss of performance by using exceptions is overrated. Today I found the following piece of code in our software: private void doSomething() { try { doSomethingElse(); } catch(Di...
I wish to throw an exception and have the following: (throw "Some text") However it seems to be ignored.
I need to write small a log analyzer application to process some log files generated by a 3rd party closed source library (having custom logger inside) used in my project. In case of an exception entry in the log I need to collect aggregated information about the methods involved along the stack trace from the top to the actual place of the exception. Unfortunately, by default Java printStac...
Sometimes I see try { }catch(Throwable e) { } And sometimes try { }catch(Exception e) { } What is the difference
Note: this question relates to stack overflows (think infinite recursion), NOT buffer overflows. If I write a program that is correct, but it accepts an input from the Internet that determines the level of recursion in a recursive function that it calls, is that potentially sufficient to allow someone to compromise the machine? I know someone might be able to crash the process by causing a s...
When I get an uncaught exception in my app and the trace prints to the logcat, is there anyway to make the entire trace print out? At the end of the trace it always has something like "...10 more". Example: 03-11 16:25:53.572: ERROR/AndroidRuntime(3297): Caused by: java.lang.ClassNotFoundException: net.notify.notifymdm.listeners.PackageInstallerReceiver in loader dalvik.system.PathClassLoader...
I'm just curious as to how it is possible for the Java JVM to sometimes inline methods that have the potential to throw exceptions. I assume that it's possible to inline at least some such methods (such as those that have array accesses and hence the potential to throw ArrayIndexOutOfBoundsExceptions). The problem I see is that if the exception actually occurs, how do you show the proper stac...
A comment (by user soc) on an answer to a question about tail call optimisation mentioned that Java 7 has a new feature called "suppressed exceptions", because of "the addition of ARM" (support for ARM CPUs?). What is a "suppressed exception" in this context? In other context a "suppressed exception" would be an exception that was caught and then ignored (almost always evil); this is clearly s...
In regular Java, you can get the text of a stack trace by passing a PrintWriter to printStackTrace. I have a feeling I know the answer to this (i.e. "No") but... Is there any way to obtain the text of a stack trace in JavaME as a String? Update: I should mention that I'm restricted to CLDC 1.0
I'd like to create a routine that does some logging, takes some other actions, and then throws an Exception. I'd like this routine to be called from many different locations. However, creating Exceptions in this routine means they will have this routine in their stack trace. I would rather the stack trace not report this utility routine. Is there a way to do this without creating the Except...
How can I get the e.printStackTrace() and store it into a String variable? I want to use the string generated by e.printStackTrace() later in my program. I'm still new to Java so I'm not too familiar with StringWriter that I think will be the solution. Or if you have any other ideas please let me know. Thanks
Is there a way to make Throwable.printStackTrace(PrintStream s) print more lines, so that I can see beyond the final line of "... 40 more" ? Thanks, Ben
I'm developing web-application with JSF. I tested it as I was able to but from time to time runtime exceptions are thrown. So, how to redirect user to special error page every time an exception is thrown (instead of displaying 500 Error with full tomcat logs)?
having the method public void foo(){ //.. } Is there a way to get the methodName (in this case foo) at runtime? I know how to get the classname via this.getClass().getName() or to get all public methods via Method[] methods = this.getClass().getMethods(); Once I have the method name the parameters would also be important as there could be several methods with same name
I am going through a socket program. In it, printStackTrace is caught by the catch block. What does printStackTrace actually do? catch(IOException ioe) { ioe.printStackTrace(); } I am unaware of it purpose. What is it used for?
I have a line number of a Java source file and want to get the sourounding method for that line number programatically. I looked into ANTLR which didn't help me much. Janino (http://www.janino.net) seems promising, I would scan and parse (and if necessary compile) the code. Then I could use JDI and ReferenceType.locationsOfLine(int lineNumber) Still I don't know how to use JDI for doing th...
I know throw new Exception(); has a pretty large overhead, since it creates a full stackTrace, etc. Does throw new Throwable(); present the same problem? Is this behaviour inherited, or does throwing a Throwable has a smaller (o no) overhead? EDIT From an * annalist* point of view, a user inserting wrong password is an exception to the normal execution order of a program. So if I have: ...
  /*
   * Copyright 1994-2006 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;
 import java.io.*;

The Throwable class is the superclass of all errors and exceptions in the Java language. Only objects that are instances of this class (or one of its subclasses) are thrown by the Java Virtual Machine or can be thrown by the Java throw statement. Similarly, only this class or one of its subclasses can be the argument type in a catch clause.

Instances of two subclasses, Error and Exception, are conventionally used to indicate that exceptional situations have occurred. Typically, these instances are freshly created in the context of the exceptional situation so as to include relevant information (such as stack trace data).

A throwable contains a snapshot of the execution stack of its thread at the time it was created. It can also contain a message string that gives more information about the error. Finally, it can contain a cause: another throwable that caused this throwable to get thrown. The cause facility is new in release 1.4. It is also known as the chained exception facility, as the cause can, itself, have a cause, and so on, leading to a "chain" of exceptions, each caused by another.

One reason that a throwable may have a cause is that the class that throws it is built atop a lower layered abstraction, and an operation on the upper layer fails due to a failure in the lower layer. It would be bad design to let the throwable thrown by the lower layer propagate outward, as it is generally unrelated to the abstraction provided by the upper layer. Further, doing so would tie the API of the upper layer to the details of its implementation, assuming the lower layer's exception was a checked exception. Throwing a "wrapped exception" (i.e., an exception containing a cause) allows the upper layer to communicate the details of the failure to its caller without incurring either of these shortcomings. It preserves the flexibility to change the implementation of the upper layer without changing its API (in particular, the set of exceptions thrown by its methods).

A second reason that a throwable may have a cause is that the method that throws it must conform to a general-purpose interface that does not permit the method to throw the cause directly. For example, suppose a persistent collection conforms to the Collection interface, and that its persistence is implemented atop java.io. Suppose the internals of the add method can throw an IOException. The implementation can communicate the details of the IOException to its caller while conforming to the Collection interface by wrapping the IOException in an appropriate unchecked exception. (The specification for the persistent collection should indicate that it is capable of throwing such exceptions.)

A cause can be associated with a throwable in two ways: via a constructor that takes the cause as an argument, or via the initCause(java.lang.Throwable) method. New throwable classes that wish to allow causes to be associated with them should provide constructors that take a cause and delegate (perhaps indirectly) to one of the Throwable constructors that takes a cause. For example:

     try {
         lowLevelOp();
     } catch (LowLevelException le) {
         throw new HighLevelException(le);  // Chaining-aware constructor
     }
 
Because the initCause method is public, it allows a cause to be associated with any throwable, even a "legacy throwable" whose implementation predates the addition of the exception chaining mechanism to Throwable. For example:
     try {
         lowLevelOp();
     } catch (LowLevelException le) {
         throw (HighLevelException)
new HighLevelException().initCause(le);  // Legacy constructor
     }
 

Prior to release 1.4, there were many throwables that had their own non-standard exception chaining mechanisms ( ExceptionInInitializerError, ClassNotFoundException, java.lang.reflect.UndeclaredThrowableException, java.lang.reflect.InvocationTargetException, java.io.WriteAbortedException, java.security.PrivilegedActionException, java.awt.print.PrinterIOException, java.rmi.RemoteException and javax.naming.NamingException). All of these throwables have been retrofitted to use the standard exception chaining mechanism, while continuing to implement their "legacy" chaining mechanisms for compatibility.

Further, as of release 1.4, many general purpose Throwable classes (for example Exception, RuntimeException, Error) have been retrofitted with constructors that take a cause. This was not strictly necessary, due to the existence of the initCause method, but it is more convenient and expressive to delegate to a constructor that takes a cause.

By convention, class Throwable and its subclasses have two constructors, one that takes no arguments and one that takes a String argument that can be used to produce a detail message. Further, those subclasses that might likely have a cause associated with them should have two more constructors, one that takes a Throwable (the cause), and one that takes a String (the detail message) and a Throwable (the cause).

Also introduced in release 1.4 is the getStackTrace() method, which allows programmatic access to the stack trace information that was previously available only in text form, via the various forms of the printStackTrace() method. This information has been added to the serialized representation of this class so getStackTrace and printStackTrace will operate properly on a throwable that was obtained by deserialization.

Author(s):
unascribed
Josh Bloch (Added exception chaining and programmatic access to stack trace in 1.4.)
Since:
JDK1.0
public class Throwable implements Serializable {
    
use serialVersionUID from JDK 1.0.2 for interoperability
    private static final long serialVersionUID = -3042686055658047285L;

    
Native code saves some indication of the stack backtrace in this slot.
    private transient Object backtrace;

    
Specific details about the Throwable. For example, for FileNotFoundException, this contains the name of the file that could not be found.

Serial:
    private String detailMessage;

    
The throwable that caused this throwable to get thrown, or null if this throwable was not caused by another throwable, or if the causative throwable is unknown. If this field is equal to this throwable itself, it indicates that the cause of this throwable has not yet been initialized.

Since:
1.4
Serial:
    private Throwable cause = this;

    
The stack trace, as returned by getStackTrace().

Since:
1.4
Serial:
    private StackTraceElement[] stackTrace;
    /*
     * This field is lazily initialized on first use or serialization and
     * nulled out when fillInStackTrace is called.
     */

    
Constructs a new throwable with null as its detail message. The cause is not initialized, and may subsequently be initialized by a call to initCause(java.lang.Throwable).

The fillInStackTrace() method is called to initialize the stack trace data in the newly created throwable.

    public Throwable() {
        fillInStackTrace();
    }

    
Constructs a new throwable with the specified detail message. The cause is not initialized, and may subsequently be initialized by a call to initCause(java.lang.Throwable).

The fillInStackTrace() method is called to initialize the stack trace data in the newly created throwable.

Parameters:
message the detail message. The detail message is saved for later retrieval by the getMessage() method.
    public Throwable(String message) {
        fillInStackTrace();
         = message;
    }

    
Constructs a new throwable with the specified detail message and cause.

Note that the detail message associated with cause is not automatically incorporated in this throwable's detail message.

The fillInStackTrace() method is called to initialize the stack trace data in the newly created throwable.

Parameters:
message the detail message (which is saved for later retrieval by the getMessage() method).
cause the cause (which is saved for later retrieval by the getCause() method). (A null value is permitted, and indicates that the cause is nonexistent or unknown.)
Since:
1.4
    public Throwable(String messageThrowable cause) {
        fillInStackTrace();
         = message;
        this. = cause;
    }

    
Constructs a new throwable with the specified cause and a detail message of (cause==null ? null : cause.toString()) (which typically contains the class and detail message of cause). This constructor is useful for throwables that are little more than wrappers for other throwables (for example, java.security.PrivilegedActionException).

The fillInStackTrace() method is called to initialize the stack trace data in the newly created throwable.

Parameters:
cause the cause (which is saved for later retrieval by the getCause() method). (A null value is permitted, and indicates that the cause is nonexistent or unknown.)
Since:
1.4
    public Throwable(Throwable cause) {
        fillInStackTrace();
         = (cause==null ? null : cause.toString());
        this. = cause;
    }

    
Returns the detail message string of this throwable.

Returns:
the detail message string of this Throwable instance (which may be null).
    public String getMessage() {
        return ;
    }

    
Creates a localized description of this throwable. Subclasses may override this method in order to produce a locale-specific message. For subclasses that do not override this method, the default implementation returns the same result as getMessage().

Returns:
The localized description of this throwable.
Since:
JDK1.1
    public String getLocalizedMessage() {
        return getMessage();
    }

    
Returns the cause of this throwable or null if the cause is nonexistent or unknown. (The cause is the throwable that caused this throwable to get thrown.)

This implementation returns the cause that was supplied via one of the constructors requiring a Throwable, or that was set after creation with the initCause(java.lang.Throwable) method. While it is typically unnecessary to override this method, a subclass can override it to return a cause set by some other means. This is appropriate for a "legacy chained throwable" that predates the addition of chained exceptions to Throwable. Note that it is not necessary to override any of the PrintStackTrace methods, all of which invoke the getCause method to determine the cause of a throwable.

Returns:
the cause of this throwable or null if the cause is nonexistent or unknown.
Since:
1.4
    public Throwable getCause() {
        return (==this ? null : );
    }

    
Initializes the cause of this throwable to the specified value. (The cause is the throwable that caused this throwable to get thrown.)

This method can be called at most once. It is generally called from within the constructor, or immediately after creating the throwable. If this throwable was created with Throwable(java.lang.Throwable) or Throwable(java.lang.String,java.lang.Throwable), this method cannot be called even once.

Parameters:
cause the cause (which is saved for later retrieval by the getCause() method). (A null value is permitted, and indicates that the cause is nonexistent or unknown.)
Returns:
a reference to this Throwable instance.
Throws:
IllegalArgumentException if cause is this throwable. (A throwable cannot be its own cause.)
IllegalStateException if this throwable was created with Throwable(java.lang.Throwable) or Throwable(java.lang.String,java.lang.Throwable), or this method has already been called on this throwable.
Since:
1.4
    public synchronized Throwable initCause(Throwable cause) {
        if (this. != this)
            throw new IllegalStateException("Can't overwrite cause");
        if (cause == this)
            throw new IllegalArgumentException("Self-causation not permitted");
        this. = cause;
        return this;
    }

    
Returns a short description of this throwable. The result is the concatenation of:
  • the name of the class of this object
  • ": " (a colon and a space)
  • the result of invoking this object's getLocalizedMessage() method
If getLocalizedMessage returns null, then just the class name is returned.

Returns:
a string representation of this throwable.
    public String toString() {
        String s = getClass().getName();
        String message = getLocalizedMessage();
        return (message != null) ? (s + ": " + message) : s;
    }

    
Prints this throwable and its backtrace to the standard error stream. This method prints a stack trace for this Throwable object on the error output stream that is the value of the field System.err. The first line of output contains the result of the toString() method for this object. Remaining lines represent data previously recorded by the method fillInStackTrace(). The format of this information depends on the implementation, but the following example may be regarded as typical:
 java.lang.NullPointerException
         at MyClass.mash(MyClass.java:9)
         at MyClass.crunch(MyClass.java:6)
         at MyClass.main(MyClass.java:3)
 
This example was produced by running the program:
 class MyClass {
     public static void main(String[] args) {
         crunch(null);
     }
     static void crunch(int[] a) {
         mash(a);
     }
     static void mash(int[] b) {
         System.out.println(b[0]);
     }
 }
 
The backtrace for a throwable with an initialized, non-null cause should generally include the backtrace for the cause. The format of this information depends on the implementation, but the following example may be regarded as typical:
 HighLevelException: MidLevelException: LowLevelException
         at Junk.a(Junk.java:13)
         at Junk.main(Junk.java:4)
 Caused by: MidLevelException: LowLevelException
         at Junk.c(Junk.java:23)
         at Junk.b(Junk.java:17)
         at Junk.a(Junk.java:11)
         ... 1 more
 Caused by: LowLevelException
         at Junk.e(Junk.java:30)
         at Junk.d(Junk.java:27)
         at Junk.c(Junk.java:21)
         ... 3 more
 
Note the presence of lines containing the characters "...". These lines indicate that the remainder of the stack trace for this exception matches the indicated number of frames from the bottom of the stack trace of the exception that was caused by this exception (the "enclosing" exception). This shorthand can greatly reduce the length of the output in the common case where a wrapped exception is thrown from same method as the "causative exception" is caught. The above example was produced by running the program:
 public class Junk {
     public static void main(String args[]) {
         try {
             a();
         } catch(HighLevelException e) {
             e.printStackTrace();
         }
     }
     static void a() throws HighLevelException {
         try {
             b();
         } catch(MidLevelException e) {
             throw new HighLevelException(e);
         }
     }
     static void b() throws MidLevelException {
         c();
     }
     static void c() throws MidLevelException {
         try {
             d();
         } catch(LowLevelException e) {
             throw new MidLevelException(e);
         }
     }
     static void d() throws LowLevelException {
        e();
     }
     static void e() throws LowLevelException {
         throw new LowLevelException();
     }
 }

 class HighLevelException extends Exception {
     HighLevelException(Throwable cause) { super(cause); }
 }

 class MidLevelException extends Exception {
     MidLevelException(Throwable cause)  { super(cause); }
 }

 class LowLevelException extends Exception {
 }
 
    public void printStackTrace() {
        printStackTrace(.);
    }

    
Prints this throwable and its backtrace to the specified print stream.

Parameters:
s PrintStream to use for output
    public void printStackTrace(PrintStream s) {
        synchronized (s) {
            s.println(this);
            StackTraceElement[] trace = getOurStackTrace();
            for (int i=0; i < trace.lengthi++)
                s.println("\tat " + trace[i]);
            Throwable ourCause = getCause();
            if (ourCause != null)
                ourCause.printStackTraceAsCause(strace);
        }
    }

    
Print our stack trace as a cause for the specified stack trace.
    private void printStackTraceAsCause(PrintStream s,
                                        StackTraceElement[] causedTrace)
    {
        // assert Thread.holdsLock(s);
        // Compute number of frames in common between this and caused
        StackTraceElement[] trace = getOurStackTrace();
        int m = trace.length-1, n = causedTrace.length-1;
        while (m >= 0 && n >=0 && trace[m].equals(causedTrace[n])) {
            m--; n--;
        }
        int framesInCommon = trace.length - 1 - m;
        s.println("Caused by: " + this);
        for (int i=0; i <= mi++)
            s.println("\tat " + trace[i]);
        if (framesInCommon != 0)
            s.println("\t... " + framesInCommon + " more");
        // Recurse if we have a cause
        Throwable ourCause = getCause();
        if (ourCause != null)
            ourCause.printStackTraceAsCause(strace);
    }

    
Prints this throwable and its backtrace to the specified print writer.

Parameters:
s PrintWriter to use for output
Since:
JDK1.1
    public void printStackTrace(PrintWriter s) {
        synchronized (s) {
            s.println(this);
            StackTraceElement[] trace = getOurStackTrace();
            for (int i=0; i < trace.lengthi++)
                s.println("\tat " + trace[i]);
            Throwable ourCause = getCause();
            if (ourCause != null)
                ourCause.printStackTraceAsCause(strace);
        }
    }

    
Print our stack trace as a cause for the specified stack trace.
    private void printStackTraceAsCause(PrintWriter s,
                                        StackTraceElement[] causedTrace)
    {
        // assert Thread.holdsLock(s);
        // Compute number of frames in common between this and caused
        StackTraceElement[] trace = getOurStackTrace();
        int m = trace.length-1, n = causedTrace.length-1;
        while (m >= 0 && n >=0 && trace[m].equals(causedTrace[n])) {
            m--; n--;
        }
        int framesInCommon = trace.length - 1 - m;
        s.println("Caused by: " + this);
        for (int i=0; i <= mi++)
            s.println("\tat " + trace[i]);
        if (framesInCommon != 0)
            s.println("\t... " + framesInCommon + " more");
        // Recurse if we have a cause
        Throwable ourCause = getCause();
        if (ourCause != null)
            ourCause.printStackTraceAsCause(strace);
    }

    
Fills in the execution stack trace. This method records within this Throwable object information about the current state of the stack frames for the current thread.

Returns:
a reference to this Throwable instance.
See also:
printStackTrace()
    public synchronized native Throwable fillInStackTrace();

    
Provides programmatic access to the stack trace information printed by printStackTrace(). Returns an array of stack trace elements, each representing one stack frame. The zeroth element of the array (assuming the array's length is non-zero) represents the top of the stack, which is the last method invocation in the sequence. Typically, this is the point at which this throwable was created and thrown. The last element of the array (assuming the array's length is non-zero) represents the bottom of the stack, which is the first method invocation in the sequence.

Some virtual machines may, under some circumstances, omit one or more stack frames from the stack trace. In the extreme case, a virtual machine that has no stack trace information concerning this throwable is permitted to return a zero-length array from this method. Generally speaking, the array returned by this method will contain one element for every frame that would be printed by printStackTrace.

Returns:
an array of stack trace elements representing the stack trace pertaining to this throwable.
Since:
1.4
    public StackTraceElement[] getStackTrace() {
        return getOurStackTrace().clone();
    }
    private synchronized StackTraceElement[] getOurStackTrace() {
        // Initialize stack trace if this is the first call to this method
        if ( == null) {
            int depth = getStackTraceDepth();
             = new StackTraceElement[depth];
            for (int i=0; i < depthi++)
                [i] = getStackTraceElement(i);
        }
        return ;
    }

    
Sets the stack trace elements that will be returned by getStackTrace() and printed by printStackTrace() and related methods. This method, which is designed for use by RPC frameworks and other advanced systems, allows the client to override the default stack trace that is either generated by fillInStackTrace() when a throwable is constructed or deserialized when a throwable is read from a serialization stream.

Parameters:
stackTrace the stack trace elements to be associated with this Throwable. The specified array is copied by this call; changes in the specified array after the method invocation returns will have no affect on this Throwable's stack trace.
Throws:
NullPointerException if stackTrace is null, or if any of the elements of stackTrace are null
Since:
1.4
    public void setStackTrace(StackTraceElement[] stackTrace) {
        StackTraceElement[] defensiveCopy = stackTrace.clone();
        for (int i = 0; i < defensiveCopy.lengthi++)
            if (defensiveCopy[i] == null)
                throw new NullPointerException("stackTrace[" + i + "]");
        this. = defensiveCopy;
    }

    
Returns the number of elements in the stack trace (or 0 if the stack trace is unavailable).
    private native int getStackTraceDepth();

    
Returns the specified element of the stack trace.

Parameters:
index index of the element to return.
Throws:
IndexOutOfBoundsException if index < 0 || index >= getStackTraceDepth()
    private native StackTraceElement getStackTraceElement(int index);
    private synchronized void writeObject(java.io.ObjectOutputStream s)
        throws IOException
    {
        getOurStackTrace();  // Ensure that stackTrace field is initialized.
        s.defaultWriteObject();
    }
New to GrepCode? Check out our FAQ X