java框架核心技术_[编织消息框架][JAVA核心技术]异常基础

Java异常体系结构

bca201a88beccd4f8eb52bb1ec9859d2.png

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构造方法

publicException(String message) {super(message);

}publicException(String message, Throwable cause) {super(message, cause);

}

publicThrowable(String message) {

fillInStackTrace();

detailMessage=message;

}publicThrowable(String message, Throwable cause) {

fillInStackTrace();

detailMessage=message;this.cause =cause;

}

第二部份:注册到异常列表

public synchronizedThrowable fillInStackTrace() {if (stackTrace != null ||backtrace!= null /*Out of protocol state*/) {//插入异常列表 0 代表插入到第一,也就是说最后一个异常放到头部

fillInStackTrace(0);

stackTrace=UNASSIGNED_STACK;

}return this;

}private native Throwable fillInStackTrace(int dummy);

第三部份:打印异常链,由于最后异常注册到头部,所以循环输出顺序是从尾到头

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 public voidprintStackTrace() {2 printStackTrace(System.err);3 }4

5 public voidprintStackTrace(PrintStream s) {6 printStackTrace(newWrappedPrintStream(s));7 }8

9 private voidprintStackTrace(PrintStreamOrWriter s) {10 //Guard against malicious overrides of Throwable.equals by11 //using a Set with identity equality semantics.

12 Set dejaVu =

13 Collections.newSetFromMap(new IdentityHashMap());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 publicStackTraceElement[] getStackTrace() {35 returngetOurStackTrace().clone();36 }37

38 private synchronizedStackTraceElement[] getOurStackTrace() {39 //Initialize stack trace field with information from40 //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 = newStackTraceElement[depth];45 for (int i=0; i < depth; i++)46 stackTrace[i] =getStackTraceElement(i);47 } else if (stackTrace == null) {48 returnUNASSIGNED_STACK;49 }50 returnstackTrace;51 }

打印异常链

4.追踪出错代码

第一个输出是最后一个异常:也就是说可以追踪到最外层出错方法

最后一个输出是第一个异常:也就是说追踪到出错的源头

来个示例说明:

1 packagecom.eyu.onequeue;2

3 public classTestException {4 public static voidmain(String[] args) {5 newTestException().demo();6 }7

8 public voida() {9 try{10 b();11 } catch(Exception e) {12 throw new RuntimeException("call a", e);13 }14

15 }16

17 public voidb() {18 try{19 c();20 } catch(Exception e) {21 throw new RuntimeException("call b", e);22 }23 }24

25 public voidc() {26 throw new RuntimeException("call c");27 }28

29 public voiddemo() {30 a();31 }32 }

Exception in thread "main" java.lang.RuntimeException: call a

at com.eyu.onequeue.TestException.a(TestException.java:12)

at com.eyu.onequeue.TestException.demo(TestException.java:30)

at com.eyu.onequeue.TestException.main(TestException.java:5)

Caused by: java.lang.RuntimeException: call b

at com.eyu.onequeue.TestException.b(TestException.java:21)

at com.eyu.onequeue.TestException.a(TestException.java:10)

... 2 more

Caused by: java.lang.RuntimeException: call c

at com.eyu.onequeue.TestException.c(TestException.java:26)

at com.eyu.onequeue.TestException.b(TestException.java:19)

... 3 more

c方法是出错的源头,a方法是调用者

小提示:当异常信息太多时,过滤条件以项目包名或类名能快速定位出错源头,一般是应用程序逻辑出错

下节利用异常开发项目

最后给出jdk常见异常

0dd03b6a3dd160ea2b54bc9892f58273.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值