Java异常体系结构
Thorwable类所有异常和错误的超类,有两个子类Error和Exception,分别表示错误和异常。
其中异常类Exception又分为运行时异常(RuntimeException)和编译时异常(checked Exception),
下面将详细讲述这些异常之间的区别与联系:
1.Error与Exception
Error是程序无法处理的错误,比如OutOfMemoryError、ThreadDeath等。这些异常发生时, Java虚拟机(JVM)一般会选择线程终止。
Exception是程序本身可以处理的异常,这种异常分两大类运行时异常和非运行时异常。 程序中应当尽可能去处理这些异常。
2.运行时异常和编译时异常
运行时异常都是RuntimeException类及其子类异常,如NullPointerException、IndexOutOfBoundsException等,
这些异常是不检查异常,程序中可以选择捕获处理,也可以不处理。这些异常一般是由程序逻辑错误引起的。
编译时异常是RuntimeException以外的异常,类型上都属于Exception类及其子类。
从程序语法角度讲是必须进行处理的异常,如果不处理,程序就不能编译通过。
3.异常链
设计目的是清晰知道程序出错调用流程,也就是传递性,一般只关心第一个跟最后一个异常信息
源码分析
分三部份:
1.构造异常
2.注册当前异常到异常列表深度坐标
3.打印异常链
第一部份:每次构造异常类时会调用顶级类Throwable构造方法
public Exception(String message) { super(message); } public Exception(String message, Throwable cause) { super(message, cause); }
public Throwable(String message) { fillInStackTrace(); detailMessage = message; } public Throwable(String message, Throwable cause) { fillInStackTrace(); detailMessage = message; this.cause = cause; }
第二部份:注册到异常列表
public synchronized Throwable fillInStackTrace() { if (stackTrace != null || backtrace != null /* Out of protocol state */ ) { //插入异常列表 0 代表插入到第一,也就是说最后一个异常放到头部 fillInStackTrace(0); stackTrace = UNASSIGNED_STACK; } return this; } private native Throwable fillInStackTrace(int dummy);
第三部份:打印异常链,由于最后异常注册到头部,所以循环输出顺序是从尾到头
1 public void printStackTrace() { 2 printStackTrace(System.err); 3 } 4 5 public void printStackTrace(PrintStream s) { 6 printStackTrace(new WrappedPrintStream(s)); 7 } 8 9 private void printStackTrace(PrintStreamOrWriter s) { 10 // Guard against malicious overrides of Throwable.equals by 11 // using a Set with identity equality semantics. 12 Set<Throwable> dejaVu = 13 Collections.newSetFromMap(new IdentityHashMap<Throwable, Boolean>()); 14 dejaVu.add(this); 15 16 synchronized (s.lock()) { 17 // Print our stack trace 18 s.println(this); 19 StackTraceElement[] trace = getOurStackTrace(); 20 for (StackTraceElement traceElement : trace) 21 s.println("\tat " + traceElement); 22 23 // Print suppressed exceptions, if any 24 for (Throwable se : getSuppressed()) 25 se.printEnclosedStackTrace(s, trace, SUPPRESSED_CAPTION, "\t", dejaVu); 26 27 // Print cause, if any 28 Throwable ourCause = getCause(); 29 if (ourCause != null) 30 ourCause.printEnclosedStackTrace(s, trace, CAUSE_CAPTION, "", dejaVu); 31 } 32 } 33 34 public StackTraceElement[] getStackTrace() { 35 return getOurStackTrace().clone(); 36 } 37 38 private synchronized StackTraceElement[] getOurStackTrace() { 39 // Initialize stack trace field with information from 40 // backtrace if this is the first call to this method 41 if (stackTrace == UNASSIGNED_STACK || 42 (stackTrace == null && backtrace != null) /* Out of protocol state */) { 43 int depth = getStackTraceDepth(); 44 stackTrace = new StackTraceElement[depth]; 45 for (int i=0; i < depth; i++) 46 stackTrace[i] = getStackTraceElement(i); 47 } else if (stackTrace == null) { 48 return UNASSIGNED_STACK; 49 } 50 return stackTrace; 51 }