📄 throwable.java
字号:
throw new IllegalArgumentException("Self-causation not permitted"); this.cause = cause; return this; } /** * Returns a short description of this throwable. * If this <code>Throwable</code> object was created with a non-null detail * message string, then the result is the concatenation of three strings: * <ul> * <li>The name of the actual class of this object * <li>": " (a colon and a space) * <li>The result of the {@link #getMessage} method for this object * </ul> * If this <code>Throwable</code> object was created with a <tt>null</tt> * detail message string, then the name of the actual class of this object * is returned. * * @return 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 * <code>Throwable</code> object on the error output stream that is * the value of the field <code>System.err</code>. The first line of * output contains the result of the {@link #toString()} method for * this object. Remaining lines represent data previously recorded by * the method {@link #fillInStackTrace()}. The format of this * information depends on the implementation, but the following * example may be regarded as typical: * <blockquote><pre> * java.lang.NullPointerException * at MyClass.mash(MyClass.java:9) * at MyClass.crunch(MyClass.java:6) * at MyClass.main(MyClass.java:3) * </pre></blockquote> * This example was produced by running the program: * <pre> * 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]); * } * } * </pre> * 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: * <pre> * 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 * </pre> * Note the presence of lines containing the characters <tt>"..."</tt>. * 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: * <pre> * 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 { * } * </pre> */ public void printStackTrace() { printStackTrace(System.err); } /** * Prints this throwable and its backtrace to the specified print stream. * * @param s <code>PrintStream</code> to use for output */ public void printStackTrace(PrintStream s) { synchronized (s) { s.println(this); /* J2SE 1.3.* style printStackTrace() printStackTrace0(s); */ StackTraceElement[] trace = getOurStackTrace(); for (int i=0; i < trace.length; i++) s.println("\tat " + trace[i]); Throwable ourCause = getCause(); if (ourCause != null) ourCause.printStackTraceAsCause(s, trace); } } /* For J2SE 1.3.* style printStackTrace(): * The given object must have a void println(char[]) method. * * private native void printStackTrace0(Object s); */ /** * 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 <= m; i++) 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(s, trace); } /** * Prints this throwable and its backtrace to the specified * print writer. * * @param s <code>PrintWriter</code> 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.length; i++) s.println("\tat " + trace[i]); Throwable ourCause = getCause(); if (ourCause != null) ourCause.printStackTraceAsCause(s, trace); } } /** * 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 <= m; i++) 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(s, trace); } /** * Fills in the execution stack trace. This method records within this * <code>Throwable</code> object information about the current state of * the stack frames for the current thread. * * @return a reference to this <code>Throwable</code> instance. * @see java.lang.Throwable#printStackTrace() */ public synchronized native Throwable fillInStackTrace(); /** * Provides programmatic access to the stack trace information printed by * {@link #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. * * <p>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 * <tt>printStackTrace</tt>. * * @return an array of stack trace elements representing the stack trace * pertaining to this throwable. * @since 1.4 */ public StackTraceElement[] getStackTrace() { return (StackTraceElement[]) getOurStackTrace().clone(); } private synchronized StackTraceElement[] getOurStackTrace() { // Initialize stack trace if this is the first call to this method if (stackTrace == null) { int depth = getStackTraceDepth(); stackTrace = new StackTraceElement[depth]; for (int i=0; i < depth; i++) stackTrace[i] = getStackTraceElement(i); } return stackTrace; } /** * Sets the stack trace elements that will be returned by * {@link #getStackTrace()} and printed by {@link #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 {@link #fillInStackTrace()} * when a throwable is constructed or deserialized when a throwable is * read from a serialization stream. * * @param stackTrace the stack trace elements to be associated with * this <code>Throwable</code>. The specified array is copied by this * call; changes in the specified array after the method invocation * returns will have no affect on this <code>Throwable</code>'s stack * trace. * * @throws NullPointerException if <code>stackTrace</code> is * <code>null</code>, or if any of the elements of * <code>stackTrace</code> are <code>null</code> * * @since 1.4 */ public void setStackTrace(StackTraceElement[] stackTrace) { StackTraceElement[] defensiveCopy = (StackTraceElement[]) stackTrace.clone(); for (int i = 0; i < defensiveCopy.length; i++) if (defensiveCopy[i] == null) throw new NullPointerException("stackTrace[" + i + "]"); this.stackTrace = 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. * * @param index index of the element to return. * @throws IndexOutOfBoundsException if <tt>index %lt; 0 || * index >= getStackTraceDepth() </tt> */ 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(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -