精华内容
下载资源
问答
  • Throwable

    2017-10-13 16:22:13
    Throwable

    java.lang.Throwable是java.lang.Error和java.lang.Exception的超类。

    1、Error

    表示系统级别的一些错误,通常来说是JVM发生了错误,也可能是由应用程序本身的一些问题所引起的。
    典型:OutOfMemoryError、 StackOverflowError

    2、Exception

    表示应用程序本身的一些异常,是由于程序编写过程中,由于逻辑不严密等等程序员自身问题所造成的异常。

    Exception又可以分成两条线,分别是编译期异常和运行期异常。

    1)编译期异常(java.lang.Exception)
    通常都是一些可以预见的异常,在程序编译期间,会强制程序员对编译期异常进行处理,通过throws语句或者try{}cathch{} 语句块来处理检测异常。
    典型:IOException、SQLException、EOFException、ClassNotFoundException、NoSuchFieldException、NoSuchMethodException。

    2)运行时异常(java.lang.RuntimeException)
    这种异常在编译期无法检测,所以在编译期间不会强制对这种异常进行处理,而这种异常会在程序运行期间抛出,一旦jvm抛出了这种异常,会引起程序的异常中断。
    典型:
    1、NullPointerException 空指针异常
    2、IndexOutOfBoundsException 下标越界异常
    3、IllegalArgumentException 传递非法参数异常
    4、ArithmeticException 算术运算异常
    5、ClassCastException 类型强制转换异常

    展开全文
  • java throwable

    2020-09-09 10:22:36
    throwable 分为 exception、error error错误 程序无法处理的错误 exception异常 程序本身可以处理的异常 exception 分为 运行时异常、非运行异常 [运行时异常、error] 为 不可检查异常 可不做处理 编译器不做强制...

    java异常

     

    throwable 分为  exception、error

    error错误 程序无法处理的错误
    exception异常 程序本身可以处理的异常

    exception 分为  运行时异常、非运行异常

    [运行时异常、error] 为 不可检查异常  可不做处理  编译器不做强制  没有异常处理 也能编译通过
    [非运行时常] 为 可检查异常  必顺处理  否则编译不通过

    展开全文
  • java Exception中Throwable和Exception之间的区别
  • Throwable源码

    2019-07-31 23:41:06
    package java.lang; import java.io.*; import java.util.*;...public class Throwable implements Serializable { 序列号版本号 private static final long serialVersionUID = -3042686055658047285L; 定...

    package java.lang;
    import  java.io.*;
    import  java.util.*;


    public class Throwable implements Serializable {
         序列号版本号
        private static final long serialVersionUID = -3042686055658047285L;

        定义了一个不序列号的Object类型的backrace属性
        private transient Object backtrace;

        定义了一个存放想想信息的变量
        private String detailMessage;


        定义一个内部类
        private static class SentinelHolder {
            //定义一个StackTraceElement//第一个参数代码声明类名,第二个参数代表方法名称,第是三天提交名。第四个是行行号
            public static final StackTraceElement STACK_TRACE_ELEMENT_SENTINEL =
                new StackTraceElement("", "", null, Integer.MIN_VALUE);

            //声明一个StackTraceElement数组值为刚刚声明的StackTraceElement对象
            public static final StackTraceElement[] STACK_TRACE_SENTINEL =
                new StackTraceElement[] {STACK_TRACE_ELEMENT_SENTINEL};
        }

        //定义一个常量StackTraceElement数组,长度为0
        private static final StackTraceElement[] UNASSIGNED_STACK = new StackTraceElement[0];

        定一个Throwable对象名字为cause 值为this
        private Throwable cause = this;

       //定义StackTraceElement数组,用上面定义的UNASSIGNED_STACK赋值
        private StackTraceElement[] stackTrace = UNASSIGNED_STACK;

        定义一个List类型为Throwable的集合常量,初始长度为0
        private static final List<Throwable> SUPPRESSED_SENTINEL =
            Collections.unmodifiableList(new ArrayList<Throwable>(0));

        定义了一个List集合类型为Throwable初始值用上面刚刚定义的来赋值
        private List<Throwable> suppressedExceptions = SUPPRESSED_SENTINEL;

        定义不能是空提示
        private static final String NULL_CAUSE_MESSAGE = "Cannot suppress a null exception.";

        定义不能是自己
        private static final String SELF_SUPPRESSION_MESSAGE = "Self-suppression not permitted";

       定义堆栈提示
        private static final String CAUSE_CAPTION = "Caused by: ";

        定义suppressed常量
        private static final String SUPPRESSED_CAPTION = "Suppressed: ";

        
        public Throwable() {
            fillInStackTrace();
        }

        public Throwable(String message) {
            fillInStackTrace();
            detailMessage = message;
        }

        public Throwable(String message, Throwable cause) {
            fillInStackTrace();
            detailMessage = message;
            this.cause = cause;
        }

        public Throwable(Throwable cause) {
            fillInStackTrace();
            detailMessage = (cause==null ? null : cause.toString());
            this.cause = cause;
        }
        protected Throwable(String message, Throwable cause,
                            boolean enableSuppression,
                            boolean writableStackTrace) {
            if (writableStackTrace) {
                fillInStackTrace();
            } else {
                stackTrace = null;
            }
            detailMessage = message;
            this.cause = cause;
            if (!enableSuppression)
                suppressedExceptions = null;
        }

        public String getMessage() {
            return detailMessage;
        }

        public String getLocalizedMessage() {
            return getMessage();
        }
        public synchronized Throwable getCause() {
            return (cause==this ? null : cause);
        }
        public synchronized Throwable initCause(Throwable cause) {
            if (this.cause != this)
                throw new IllegalStateException("Can't overwrite cause with " +
                                                Objects.toString(cause, "a null"), this);
            if (cause == this)
                throw new IllegalArgumentException("Self-causation not permitted", this);
            this.cause = cause;
            return this;
        }
        public String toString() {
            String s = getClass().getName();
            String message = getLocalizedMessage();
            return (message != null) ? (s + ": " + message) : s;
        }
        public void printStackTrace() {
            printStackTrace(System.err);
        }
        public void printStackTrace(PrintStream s) {
            printStackTrace(new WrappedPrintStream(s));
        }
        private void printStackTrace(PrintStreamOrWriter s) {
            // Guard against malicious overrides of Throwable.equals by
            // using a Set with identity equality semantics.
            Set<Throwable> dejaVu =
                Collections.newSetFromMap(new IdentityHashMap<Throwable, Boolean>());
            dejaVu.add(this);

            synchronized (s.lock()) {
                // Print our stack trace
                s.println(this);
                StackTraceElement[] trace = getOurStackTrace();
                for (StackTraceElement traceElement : trace)
                    s.println("\tat " + traceElement);

                // Print suppressed exceptions, if any
                for (Throwable se : getSuppressed())
                    se.printEnclosedStackTrace(s, trace, SUPPRESSED_CAPTION, "\t", dejaVu);

                // Print cause, if any
                Throwable ourCause = getCause();
                if (ourCause != null)
                    ourCause.printEnclosedStackTrace(s, trace, CAUSE_CAPTION, "", dejaVu);
            }
        }
        private void printEnclosedStackTrace(PrintStreamOrWriter s,
                                             StackTraceElement[] enclosingTrace,
                                             String caption,
                                             String prefix,
                                             Set<Throwable> dejaVu) {
            assert Thread.holdsLock(s.lock());
            if (dejaVu.contains(this)) {
                s.println("\t[CIRCULAR REFERENCE:" + this + "]");
            } else {
                dejaVu.add(this);
                // Compute number of frames in common between this and enclosing trace
                StackTraceElement[] trace = getOurStackTrace();
                int m = trace.length - 1;
                int n = enclosingTrace.length - 1;
                while (m >= 0 && n >=0 && trace[m].equals(enclosingTrace[n])) {
                    m--; n--;
                }
                int framesInCommon = trace.length - 1 - m;

                // Print our stack trace
                s.println(prefix + caption + this);
                for (int i = 0; i <= m; i++)
                    s.println(prefix + "\tat " + trace[i]);
                if (framesInCommon != 0)
                    s.println(prefix + "\t... " + framesInCommon + " more");

                // Print suppressed exceptions, if any
                for (Throwable se : getSuppressed())
                    se.printEnclosedStackTrace(s, trace, SUPPRESSED_CAPTION,
                                               prefix +"\t", dejaVu);

                // Print cause, if any
                Throwable ourCause = getCause();
                if (ourCause != null)
                    ourCause.printEnclosedStackTrace(s, trace, CAUSE_CAPTION, prefix, dejaVu);
            }
        }
        public void printStackTrace(PrintWriter s) {
            printStackTrace(new WrappedPrintWriter(s));
        
        private abstract static class PrintStreamOrWriter {
            /** Returns the object to be locked when using this StreamOrWriter */
            abstract Object lock();

            /** Prints the specified string as a line on this StreamOrWriter */
            abstract void println(Object o);
        }

        private static class WrappedPrintStream extends PrintStreamOrWriter {
            private final PrintStream printStream;

            WrappedPrintStream(PrintStream printStream) {
                this.printStream = printStream;
            }

            Object lock() {
                return printStream;
            }

            void println(Object o) {
                printStream.println(o);
            }
        }

        private static class WrappedPrintWriter extends PrintStreamOrWriter {
            private final PrintWriter printWriter;

            WrappedPrintWriter(PrintWriter printWriter) {
                this.printWriter = printWriter;
            }

            Object lock() {
                return printWriter;
            }

            void println(Object o) {
                printWriter.println(o);
            }
        }
        public synchronized Throwable fillInStackTrace() {
            if (stackTrace != null ||
                backtrace != null /* Out of protocol state */ ) {
                fillInStackTrace(0);
                stackTrace = UNASSIGNED_STACK;
            }
            return this;
        }

        private native Throwable fillInStackTrace(int dummy);

        
        public StackTraceElement[] getStackTrace() {
            return getOurStackTrace().clone();
        }

        private synchronized StackTraceElement[] getOurStackTrace() {
            // Initialize stack trace field with information from
            // backtrace if this is the first call to this method
            if (stackTrace == UNASSIGNED_STACK ||
                (stackTrace == null && backtrace != null) /* Out of protocol state */) {
                int depth = getStackTraceDepth();
                stackTrace = new StackTraceElement[depth];
                for (int i=0; i < depth; i++)
                    stackTrace[i] = getStackTraceElement(i);
            } else if (stackTrace == null) {
                return UNASSIGNED_STACK;
            }
            return stackTrace;
        }

        public void setStackTrace(StackTraceElement[] stackTrace) {
            // Validate argument
            StackTraceElement[] defensiveCopy = stackTrace.clone();
            for (int i = 0; i < defensiveCopy.length; i++) {
                if (defensiveCopy[i] == null)
                    throw new NullPointerException("stackTrace[" + i + "]");
            }

            synchronized (this) {
                if (this.stackTrace == null && // Immutable stack
                    backtrace == null) // Test for out of protocol state
                    return;
                this.stackTrace = defensiveCopy;
            }
        }

        native int getStackTraceDepth();

        native StackTraceElement getStackTraceElement(int index);

        private void readObject(ObjectInputStream s)
            throws IOException, ClassNotFoundException {
            s.defaultReadObject();     // read in all fields
            if (suppressedExceptions != null) {
                List<Throwable> suppressed = null;
                if (suppressedExceptions.isEmpty()) {
                    // Use the sentinel for a zero-length list
                    suppressed = SUPPRESSED_SENTINEL;
                } else { // Copy Throwables to new list
                    suppressed = new ArrayList<>(1);
                    for (Throwable t : suppressedExceptions) {
                        // Enforce constraints on suppressed exceptions in
                        // case of corrupt or malicious stream.
                        if (t == null)
                            throw new NullPointerException(NULL_CAUSE_MESSAGE);
                        if (t == this)
                            throw new IllegalArgumentException(SELF_SUPPRESSION_MESSAGE);
                        suppressed.add(t);
                    }
                }
                suppressedExceptions = suppressed;
            } // else a null suppressedExceptions field remains null

            
            if (stackTrace != null) {
                if (stackTrace.length == 0) {
                    stackTrace = UNASSIGNED_STACK.clone();
                }  else if (stackTrace.length == 1 &&
                            // Check for the marker of an immutable stack trace
                            SentinelHolder.STACK_TRACE_ELEMENT_SENTINEL.equals(stackTrace[0])) {
                    stackTrace = null;
                } else { // Verify stack trace elements are non-null.
                    for(StackTraceElement ste : stackTrace) {
                        if (ste == null)
                            throw new NullPointerException("null StackTraceElement in serial stream. ");
                    }
                }
            } else {
                // A null stackTrace field in the serial form can result
                // from an exception serialized without that field in
                // older JDK releases; treat such exceptions as having
                // empty stack traces.
                stackTrace = UNASSIGNED_STACK.clone();
            }
        }

        private synchronized void writeObject(ObjectOutputStream s)
            throws IOException {
            // Ensure that the stackTrace field is initialized to a
            // non-null value, if appropriate.  As of JDK 7, a null stack
            // trace field is a valid value indicating the stack trace
            // should not be set.
            getOurStackTrace();

            StackTraceElement[] oldStackTrace = stackTrace;
            try {
                if (stackTrace == null)
                    stackTrace = SentinelHolder.STACK_TRACE_SENTINEL;
                s.defaultWriteObject();
            } finally {
                stackTrace = oldStackTrace;
            }
        }

        public final synchronized void addSuppressed(Throwable exception) {
            if (exception == this)
                throw new IllegalArgumentException(SELF_SUPPRESSION_MESSAGE, exception);

            if (exception == null)
                throw new NullPointerException(NULL_CAUSE_MESSAGE);

            if (suppressedExceptions == null) // Suppressed exceptions not recorded
                return;

            if (suppressedExceptions == SUPPRESSED_SENTINEL)
                suppressedExceptions = new ArrayList<>(1);

            suppressedExceptions.add(exception);
        }

        private static final Throwable[] EMPTY_THROWABLE_ARRAY = new Throwable[0];

        public final synchronized Throwable[] getSuppressed() {
            if (suppressedExceptions == SUPPRESSED_SENTINEL ||
                suppressedExceptions == null)
                return EMPTY_THROWABLE_ARRAY;
            else
                return suppressedExceptions.toArray(EMPTY_THROWABLE_ARRAY);
        }
    }

    展开全文
  • JDK Throwable

    2019-09-30 22:21:49
    Throwable 1. 使用大量数组和List常量: private static final StackTraceElement[] UNASSIGNED_STACK = new StackTraceElement[0]; 2. 使用静态内部类 3. 拥有自身类的成员对象: cause, suppressedExceptions ...

    Throwable

    1. 使用大量数组和List常量: private static final StackTraceElement[] UNASSIGNED_STACK = new StackTraceElement[0];

    2. 使用静态内部类

    3. 拥有自身类的成员对象: cause, suppressedExceptions 

    public class Throwable implements Serializable {
    
        // Array & List常量
        private static final StackTraceElement[] UNASSIGNED_STACK = new StackTraceElement[0];
        private StackTraceElement[] stackTrace = UNASSIGNED_STACK;
    
        private static final Throwable[] EMPTY_THROWABLE_ARRAY = new Throwable[0];
    
        private static final List<Throwable> SUPPRESSED_SENTINEL = Collections
                .unmodifiableList(new ArrayList<Throwable>(0));
        private List<Throwable> suppressedExceptions = SUPPRESSED_SENTINEL;
        
        private Throwable cause = this;
    
        // 静态内部内
        private static class SentinelHolder {
    
            public static final StackTraceElement STACK_TRACE_ELEMENT_SENTINEL = new StackTraceElement("", "", null, Integer.MIN_VALUE);
            // 输出时当stackTrace为空时,即赋予该值
            public static final StackTraceElement[] STACK_TRACE_SENTINEL = new StackTraceElement[] { STACK_TRACE_ELEMENT_SENTINEL };
        }
    
        public final synchronized void addSuppressed(Throwable exception) {
            if (exception == this)
                throw new IllegalArgumentException(SELF_SUPPRESSION_MESSAGE, exception);
    
            if (exception == null)
                throw new NullPointerException(NULL_CAUSE_MESSAGE);
    
            if (suppressedExceptions == null)  
                return;
    
            if (suppressedExceptions == SUPPRESSED_SENTINEL) // suppressedExceptions等于初始值,即创建list
                suppressedExceptions = new ArrayList<>(1);
    
            suppressedExceptions.add(exception);
        }
    
        public void printStackTrace(PrintWriter s) {
            printStackTrace(new WrappedPrintWriter(s));      // 使用静态内部类
        }
    
        private void printStackTrace(PrintStreamOrWriter s) {
            Set<Throwable> dejaVu = Collections.newSetFromMap(new IdentityHashMap<Throwable, Boolean>());
            dejaVu.add(this);
    
            synchronized (s.lock()) {
                // Print our stack trace
                s.println(this);
                StackTraceElement[] trace = getOurStackTrace();
                for (StackTraceElement traceElement : trace)
                    s.println("\tat " + traceElement);
    
                for (Throwable se : getSuppressed())
                    se.printEnclosedStackTrace(s, trace, SUPPRESSED_CAPTION, "\t", dejaVu);
    
                Throwable ourCause = getCause();
                if (ourCause != null)
                    ourCause.printEnclosedStackTrace(s, trace, CAUSE_CAPTION, "", dejaVu);
            }
        }
    
        private abstract static class PrintStreamOrWriter {
            abstract Object lock();
    
            abstract void println(Object o);
        }
    
        private static class WrappedPrintStream extends PrintStreamOrWriter {
            private final PrintStream printStream;
    
            WrappedPrintStream(PrintStream printStream) {
                this.printStream = printStream;
            }
    
            Object lock() {
                return printStream;
            }
    
            void println(Object o) {
                printStream.println(o);
            }
        }
    
        private synchronized StackTraceElement[] getOurStackTrace() {
            if (stackTrace == UNASSIGNED_STACK || (stackTrace == null && backtrace != null){
                int depth = getStackTraceDepth();
                stackTrace = new StackTraceElement[depth];
                for (int i = 0; i < depth; i++)
                    stackTrace[i] = getStackTraceElement(i);
            } else if (stackTrace == null) {
                return UNASSIGNED_STACK;
            }
            return stackTrace;
        }
    
        private void printEnclosedStackTrace(PrintStreamOrWriter s,
                StackTraceElement[] enclosingTrace, String caption, String prefix, Set<Throwable> dejaVu) {
            assert Thread.holdsLock(s.lock());
            if (dejaVu.contains(this)) {
                s.println("\t[CIRCULAR REFERENCE:" + this + "]");
            } else {
                dejaVu.add(this);
                // Compute number of frames in common between this and enclosing
                StackTraceElement[] trace = getOurStackTrace();
                int m = trace.length - 1;
                int n = enclosingTrace.length - 1;
                while (m >= 0 && n >= 0 && trace[m].equals(enclosingTrace[n])) {
                    m--;
                    n--;
                }
                int framesInCommon = trace.length - 1 - m;
    
                // Print our stack trace
                s.println(prefix + caption + this);
                for (int i = 0; i <= m; i++)
                    s.println(prefix + "\tat " + trace[i]);
                if (framesInCommon != 0)
                    s.println(prefix + "\t... " + framesInCommon + " more");
    
                // Print suppressed exceptions, if any
                for (Throwable se : getSuppressed())
                    se.printEnclosedStackTrace(s, trace, SUPPRESSED_CAPTION, prefix + "\t", dejaVu);
    
                // Print cause, if any
                Throwable ourCause = getCause();
                if (ourCause != null)
                    ourCause.printEnclosedStackTrace(s, trace, CAUSE_CAPTION, prefix, dejaVu);
            }
        }
        
        public final synchronized Throwable[] getSuppressed() {
            if (suppressedExceptions == SUPPRESSED_SENTINEL ||
                suppressedExceptions == null)
                return EMPTY_THROWABLE_ARRAY;
            else
                return suppressedExceptions.toArray(EMPTY_THROWABLE_ARRAY);
        }
        
         public synchronized Throwable getCause() {
             return (cause==this ? null : cause);
         }
    
    }

     

    转载于:https://www.cnblogs.com/anxiao/p/6593919.html

    展开全文
  • Throwable异常

    2019-11-10 19:37:22
    Throwable所有异常和错误的父类 有两个子类 Error 错误 Error表示系统级的错误和程序无法通过修改程序处理的异常,内存泄露。 Exception 异常 Exception表示需要捕捉或者需要程序进行处理的异常,如果程序正常执行,...
  • 代码: //首先有个变量case private Throwable cause = this;... public synchronized Throwable initCause(Throwable cause) { // 保证只能调用一次 if (this.cause != this) throw new IllegalStateExc
  • Throwable.initCause

    2020-01-02 17:36:54
    public synchronized Throwable initCause(Throwable cause)
  • Throwable PHP7

    2020-12-09 02:23:35
    t catch Throwable instead Exception <h2>Possible Solution <p>refactor all try-catch <h2>My Environment <ul><li>...
  • Class Throwable

    2017-04-18 23:05:01
    Java官方API:https://docs.oracle.com/javase/8/docs/api/... compact1, compact2, compact3 java.lang ...Class Throwable java.lang.Object java.lang.Throwable All Implemented Interface
  • java Throwable接口

    2020-06-21 12:17:38
    步骤1:Throwable步骤2:练习-Throwable步骤3:答案-Throwable 步骤1:Throwable Throwable是类,Exception和Error都继承了该类 所以在捕捉的时候,也可以使用Throwable进行捕捉 如图: 异常分Error和Exception ...
  • Throwable

    2018-08-13 15:07:00
    1.Throwable是所有异常的基类(父类),两个子类Error和Exception ①Error:java运行时系统的内部错误或资源耗尽错误,应用程序不应该抛出这种类型的对象,一旦发生这种异常除了是程序安全地终止之外,没有其他办法...
  • Throwable not supported

    2020-12-02 02:08:56
    They should accept <code>\Throwable</code>. <p>https://github.com/php-http/httplug/blob/master/src/Exception/NetworkException.php#L24</p><p>该提问来源于开源项目:php-http/httplug</p></div>
  • throwable 本文是有关异常的教程。 但不是通常的一种。 其中有许多内容可以告诉您异常的含义,如何抛出异常,捕获异常,已检查异常和运行时异常之间的区别,等等。 没有必要了。 这对您来说也很无聊。 如果没有,...
  • Add the Throwable interface

    2020-12-05 10:48:19
    t implement the <code>Throwable</code> interface, only children of the polyfilled <code>Error</code> class. - While a <code>catch (Throwable $t)</code> statement won't produce an error, it will ...
  • <div><p>Laravel's exception handler requires Exception, but Throwable was passed</p><p>该提问来源于开源项目:nWidart/laravel-modules</p></div>
  • Feature: throwable support

    2020-12-06 00:47:10
    <div><p>This PR introduces support for PHP 7 <code>Throwable</code> errors. This means that anything that would otherwise crash bernardphp hard causes a <code>RejectEnvelopeEvent</code> to be fired....

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 14,703
精华内容 5,881
关键字:

throwable