Throwable

  • 属性说明
/**
* Java 语言中所有错误和异常的基类,此类及其子类才能通过 throws 被 JVM 虚拟机抛出。
* @since 1.0
*/
public class Throwable implements Serializable {
/** use serialVersionUID from JDK 1.0.2 for interoperability */
private static final long serialVersionUID = -3042686055658047285L; /**
* JVM 会保存一些栈回溯信息到此对象上
*/
private transient Object backtrace; /**
* 异常的细节信息
*/
private String detailMessage; /**
* 共享的空堆栈信息
*/
private static final StackTraceElement[] UNASSIGNED_STACK = new StackTraceElement[0]; /**
* 引起此 Throwable 抛出的源 Throwable 对象
* @since 1.4
*/
private Throwable cause = this; /**
* 此 Throwable 关联的堆栈信息
* @serial
* @since 1.4
*/
private StackTraceElement[] stackTrace = UNASSIGNED_STACK; /**
* 栈回溯的深度
*/
private transient int depth; // Setting this static field introduces an acceptable
// initialization dependency on a few java.util classes.
private static final List<Throwable> SUPPRESSED_SENTINEL = Collections.emptyList(); /**
* 被抑制的异常对象列表
* @serial
* @since 1.7
*/
private List<Throwable> suppressedExceptions = SUPPRESSED_SENTINEL; /** 用于抑制空指针异常的消息 */
private static final String NULL_CAUSE_MESSAGE = "Cannot suppress a null exception."; /** Message for trying to suppress oneself. */
private static final String SELF_SUPPRESSION_MESSAGE = "Self-suppression not permitted"; /** 导致异常堆栈跟踪的标题信息 */
private static final String CAUSE_CAPTION = "Caused by: "; /** 被抑制的异常堆栈跟踪的标题信息 */
private static final String SUPPRESSED_CAPTION = "Suppressed: ";
  • 实例化
    /**
* 创建一个无描述信息的 Throwable 对象
*/
public Throwable() {
fillInStackTrace();
} /**
* 创建一个持有指定描述信息 message 的 Throwable 对象
* @param message 详细的信息
*/
public Throwable(String message) {
fillInStackTrace();
detailMessage = message;
} /**
* 创建一个持有详细信息和异常来源的 Throwable 对象
* @param message 详细描述信息
* @param cause 引起此 Throwable 的源 Throwable 对象
* @since 1.4
*/
public Throwable(String message, Throwable cause) {
fillInStackTrace();
detailMessage = message;
this.cause = cause;
} /**
* 创建一个持有源 Throwable 实例的 Throwable 对象
*
* @param cause 源 Throwable 对象
* @since 1.4
*/
public Throwable(Throwable cause) {
fillInStackTrace();
detailMessage = cause==null ? null : cause.toString();
this.cause = cause;
} /**
* 填充此 Throwable 实例的堆栈跟踪信息
*/
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 void printStackTrace() {
printStackTrace(System.err);
} /**
* 将此 Throwable 的堆栈信息输出到指定的 PrintStream s 中
*
* @param s 用于输出的目标 {@code PrintStream}
*/
public void printStackTrace(PrintStream s) {
printStackTrace(new WrappedPrintStream(s));
} private void printStackTrace(PrintStreamOrWriter s) {
final Set<Throwable> dejaVu = Collections.newSetFromMap(new IdentityHashMap<>());
dejaVu.add(this); synchronized (s.lock()) {
// 打印此 Throwable 的信息
s.println(this);
// 打印相关的堆栈信息
final StackTraceElement[] trace = getOurStackTrace();
for (final StackTraceElement traceElement : trace) {
s.println("\tat " + traceElement);
} // 打印抑制的异常信息
for (final Throwable se : getSuppressed()) {
se.printEnclosedStackTrace(s, trace, SUPPRESSED_CAPTION, "\t", dejaVu);
} // 打印异常原因
final Throwable ourCause = getCause();
if (ourCause != null) {
ourCause.printEnclosedStackTrace(s, trace, CAUSE_CAPTION, "", dejaVu);
}
}
} /**
* 将此 Throwable 的堆栈信息打印到 PrintWriter s 中
*
* @param s 用于输出的 {@code PrintWriter}
* @since 1.1
*/
public void printStackTrace(PrintWriter s) {
printStackTrace(new WrappedPrintWriter(s));
} /**
* 封装 PrintStream and PrintWriter
*/
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;
} @Override
Object lock() {
return printStream;
} @Override
void println(Object o) {
printStream.println(o);
}
} private static class WrappedPrintWriter extends PrintStreamOrWriter {
private final PrintWriter printWriter; WrappedPrintWriter(PrintWriter printWriter) {
this.printWriter = printWriter;
} @Override
Object lock() {
return printWriter;
} @Override
void println(Object o) {
printWriter.println(o);
}
}

Throwable 源码阅读的更多相关文章

  1. 源码阅读系列:EventBus

    title: 源码阅读系列:EventBus date: 2016-12-22 16:16:47 tags: 源码阅读 --- EventBus 是人们在日常开发中经常会用到的开源库,即使是不直接用的 ...

  2. EventBus源码解析 源码阅读记录

    EventBus源码阅读记录 repo地址: greenrobot/EventBus EventBus的构造 双重加锁的单例. static volatile EventBus defaultInst ...

  3. Bean实例化(Spring源码阅读)-我们到底能走多远系列(33)

    我们到底能走多远系列(33) 扯淡: 各位:    命运就算颠沛流离   命运就算曲折离奇   命运就算恐吓着你做人没趣味   别流泪 心酸 更不应舍弃   ... 主题: Spring源码阅读还在继 ...

  4. Netty源码阅读(一) ServerBootstrap启动

    Netty源码阅读(一) ServerBootstrap启动 转自我的Github Netty是由JBOSS提供的一个java开源框架.Netty提供异步的.事件驱动的网络应用程序框架和工具,用以快速 ...

  5. 初始化IoC容器(Spring源码阅读)

    初始化IoC容器(Spring源码阅读) 我们到底能走多远系列(31) 扯淡: 有个问题一直想问:各位你们的工资剩下来会怎么处理?已婚的,我知道工资永远都是不够的.未婚的你们,你们是怎么分配工资的? ...

  6. Spark源码阅读之存储体系--存储体系概述与shuffle服务

    一.概述 根据<深入理解Spark:核心思想与源码分析>一书,结合最新的spark源代码master分支进行源码阅读,对新版本的代码加上自己的一些理解,如有错误,希望指出. 1.块管理器B ...

  7. Mina源码阅读笔记(四)—Mina的连接IoConnector2

    接着Mina源码阅读笔记(四)-Mina的连接IoConnector1,,我们继续: AbstractIoAcceptor: 001 package org.apache.mina.core.rewr ...

  8. Spring源码阅读笔记

    前言 作为一个Java开发者,工作了几年后,越发觉力有点不从心了,技术的世界实在是太过于辽阔了,接触的东西越多,越感到前所未有的恐慌. 每天捣鼓这个捣鼓那个,结果回过头来,才发现这个也不通,那个也不精 ...

  9. Rpc框架dubbo-client(v2.6.3) 源码阅读(二)

    接上一篇 dubbo-server 之后,再来看一下 dubbo-client 是如何工作的. dubbo提供者服务示例, 其结构是这样的!dubbo://192.168.11.6:20880/com ...

随机推荐

  1. 【异常】553 Mail from must equal authorized user

    1 详细异常打印 2019-08-12 14:54:42,178 ERROR org.apache.camel.processor.DefaultErrorHandler: Failed delive ...

  2. python常用模块:logging、hashlib、re

    今日内容主要有:一.logging模块二.logging模块的使用三.hashlib模块四.re模块 一.logging模块 import logging # 1 日志的级别 logging.debu ...

  3. 数据驱动——ddt

    1: pip3 install ddt 2: @ddt 装饰 @data((2,3),(4,5)) 支持列表,元祖,字典 @unpack 解压数据   1 import unittest 2 from ...

  4. 8080 端口被占用的解决方法 netstat -ano;taskkill (命令行)

    8080 端口被占用的解决方法 netstat -ano:taskkill (命令行) (ano 和 aon 都可以) 打开命令行: (1)netstat -ano 可查看端口使用情况,记住 PID ...

  5. Java运行环境绿色部署配置

    这个Java的绿色安装配置,还有从未自己的使用电脑说起来. 最近电脑运行慢,很长时间没有清理及维护了,而且有可能中毒或木马了,所以就把系统进行了Ghost还原了,所以原来安装的jdk环境也无法使用了, ...

  6. 报错:required string parameter XXX is not present

    报错:required string parameter XXX is not present 不同工具发起的get/delete请求,大多数不支持@RequestParam,只支持@PathVari ...

  7. Swagger2常用注解和使用方法

    一   引入maven依赖 <!--整合Swagger2--> <dependency> <groupId>com.spring4all</groupId&g ...

  8. electron-vue [Vue warn]: Failed to resolve directive: decorator

    electron-vue引入ant-desigin-vue使用ant自定义指令 v-decorator报销 <a-form-item> <a-input v-decorator=&q ...

  9. isset和empty以及is_null区别

    2.empty,isset首先都会检查变量是否存在,然后对变量值进行检测.而is_null 和 “参数本身”只是直接检查变量值,是否为null,因此如果变量未定义就会出现错误! 3.isset():仅 ...

  10. UVA 315 求割点 模板 Tarjan

    D - D Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Pract ...