当前位置: 首页 > news >正文

Java异常体系结构核心解析-Throwable

资料不在于多,而在于精。好资料、好书,我们站在巨人的肩膀上前行,可以少走很多弯路
通过搜索引擎找到自己需要的最好最权威信息,是一种很重要的能力
Java源代码和官方资料Java™ Tutorials

Java异常体系结构,是一种分层/层次结构树模型。
异常的根类是 java.lang.Throwable,核心数据结构/模型和实现都在于此类。了解她们对理解异常信息很关键。

其子类 java.lang.Exception、java.lang.RuntimeException、java.lang.Error 都是标签类。

java.lang.Throwable

Throwable 是异常的根类,核心数据结构/模型和实现都在于此类。了解她们对理解异常信息很关键

/*** The {@code Throwable} class is the superclass of all errors and* exceptions in the Java language. Only objects that are instances of this* class (or one of its subclasses) are thrown by the Java Virtual Machine or* can be thrown by the Java {@code throw} statement. Similarly, only* this class or one of its subclasses can be the argument type in a* {@code catch} clause.** For the purposes of compile-time checking of exceptions, {@code* Throwable} and any subclass of {@code Throwable} that is not also a* subclass of either {@link RuntimeException} or {@link Error} are* regarded as checked exceptions.** <p>Instances of two subclasses, {@link java.lang.Error} and* {@link java.lang.Exception}, are conventionally used to indicate* that exceptional situations have occurred. Typically, these instances* are freshly created in the context of the exceptional situation so* as to include relevant information (such as stack trace data).** @author  unascribed* @author  Josh Bloch (Added exception chaining and programmatic access to*          stack trace in 1.4.)* @jls 11.2 Compile-Time Checking of Exceptions* @since JDK1.0*/
public class Throwable implements Serializable {/*** Specific details about the Throwable.*/private String detailMessage;/*** The throwable that caused this throwable to get thrown, or null if this* throwable was not caused by another throwable, or if the causative* throwable is unknown.*/private Throwable cause = this;/*** The stack trace, as returned by {@link #getStackTrace()}.*/private StackTraceElement[] stackTrace = UNASSIGNED_STACK;/*** The list of suppressed exceptions, as returned by {@link #getSuppressed()}.*/private List<Throwable> suppressedExceptions = SUPPRESSED_SENTINEL;/** Caption  for labeling causative exception stack traces */private static final String CAUSE_CAPTION = "Caused by: ";/** Caption for labeling suppressed exception stack traces */private static final String SUPPRESSED_CAPTION = "Suppressed: ";/*** Constructs a new throwable with the specified detail message and* cause.  <p>Note that the detail message associated with* {@code cause} is <i>not</i> automatically incorporated in* this throwable's detail message.** <p>The {@link #fillInStackTrace()} method is called to initialize* the stack trace data in the newly created throwable.*/public Throwable(String message, Throwable cause) {fillInStackTrace();detailMessage = message;this.cause = cause;}/*** Returns a short description of this throwable.* The result is the concatenation of:* <ul>* <li> the {@linkplain Class#getName() name} of the class of this object* <li> ": " (a colon and a space)* <li> the result of invoking this object's {@link #getLocalizedMessage}*      method* </ul>* If {@code getLocalizedMessage} returns {@code null}, then just* the class name is returned.*/public String toString() {String s = getClass().getName();String message = getLocalizedMessage();return (message != null) ? (s + ": " + message) : 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 traces.println(this);StackTraceElement[] trace = getOurStackTrace();for (StackTraceElement traceElement : trace)s.println("\tat " + traceElement);// Print suppressed exceptions, if anyfor (Throwable se : getSuppressed())se.printEnclosedStackTrace(s, trace, SUPPRESSED_CAPTION, "\t", dejaVu);// Print cause, if anyThrowable ourCause = getCause();if (ourCause != null)ourCause.printEnclosedStackTrace(s, trace, CAUSE_CAPTION, "", dejaVu);}}}

java.lang.StackTraceElement

其中 StackTraceElement 表示调用栈跟踪元素,了解其核心数据结构/模型对理解异常信息也很关键。

/*** An element in a stack trace, as returned by {@link* Throwable#getStackTrace()}.  Each element represents a single stack frame.* All stack frames except for the one at the top of the stack represent* a method invocation.  The frame at the top of the stack represents the* execution point at which the stack trace was generated.  Typically,* this is the point at which the throwable corresponding to the stack trace* was created.** @since  1.4* @author Josh Bloch*/
public final class StackTraceElement implements java.io.Serializable {private String declaringClass;private String methodName;private String fileName;private int    lineNumber;/*** Creates a stack trace element representing the specified execution* point.*/public StackTraceElement(String declaringClass, String methodName,String fileName, int lineNumber) {this.declaringClass = Objects.requireNonNull(declaringClass, "Declaring class is null");this.methodName     = Objects.requireNonNull(methodName, "Method name is null");this.fileName       = fileName;this.lineNumber     = lineNumber;}/*** Returns a string representation of this stack trace element.  The* format of this string depends on the implementation, but the following* examples may be regarded as typical:* <ul>* <li>*   {@code "MyClass.mash(MyClass.java:9)"} - Here, {@code "MyClass"}*   is the <i>fully-qualified name</i> of the class containing the*   execution point represented by this stack trace element,*   {@code "mash"} is the name of the method containing the execution*   point, {@code "MyClass.java"} is the source file containing the*   execution point, and {@code "9"} is the line number of the source*   line containing the execution point.* <li>*   {@code "MyClass.mash(MyClass.java)"} - As above, but the line*   number is unavailable.* <li>*   {@code "MyClass.mash(Unknown Source)"} - As above, but neither*   the file name nor the line  number are available.* <li>*   {@code "MyClass.mash(Native Method)"} - As above, but neither*   the file name nor the line  number are available, and the method*   containing the execution point is known to be a native method.* </ul>* @see    Throwable#printStackTrace()*/public String toString() {return getClassName() + "." + methodName +(isNativeMethod() ? "(Native Method)" :(fileName != null && lineNumber >= 0 ?"(" + fileName + ":" + lineNumber + ")" :(fileName != null ?  "("+fileName+")" : "(Unknown Source)")));}}

异常日志解读示例

实践出真知识,才能真正地理解!

异常日志解读

com.alibaba.xxx.xxx.common.exception.ServiceException: waybill rule is null by DISTRIBUTOR_30474702at com.alibaba.xxx.xxx.biz.common.WaybillAssistUtils.getExportWaybillRuleDO(WaybillAssistUtils.java:239)第一行日志
com.alibaba.xxx.xxx.common.exception.ServiceException: waybill rule is null by DISTRIBUTOR_30474702
// 参考 java.lang.Throwable#toString
getClass().getName():com.alibaba.xxx.xxx.common.exception.ServiceException
getLocalizedMessage()=detailMessage:waybill rule is null by DISTRIBUTOR_30474702第二行日志at com.alibaba.xxx.xxx.biz.common.WaybillAssistUtils.getExportWaybillRuleDO(WaybillAssistUtils.java:239)
// 参考 java.lang.Throwable#printStackTrace(java.lang.Throwable.PrintStreamOrWriter)
//            StackTraceElement[] trace = getOurStackTrace();
//            for (StackTraceElement traceElement : trace)
//                s.println("\tat " + traceElement);
"\tat ":	at 
traceElement:com.alibaba.xxx.xxx.biz.common.WaybillAssistUtils.getExportWaybillRuleDO(WaybillAssistUtils.java:239)
// 参考 java.lang.StackTraceElement#toString
getClassName() + "." + methodName:com.alibaba.xxx.xxx.biz.common.WaybillAssistUtils.getExportWaybillRuleDO
getClassName()=declaringClass:com.alibaba.xxx.xxx.biz.common.WaybillAssistUtils
methodName:getExportWaybillRuleDO
"(" + fileName + ":" + lineNumber + ")":(WaybillAssistUtils.java:239)
fileName:WaybillAssistUtils.java
lineNumber:239

进阶阅读资料

  • Java语言规范和源代码
  • 官方资料Java™ Tutorials - https://docs.oracle.com/javase/tutorial/
  • Java™ Tutorials 的 Lesson: Exceptions 对异常体系讲解的通俗易懂 - https://docs.oracle.com/javase/tutorial/essential/exceptions/index.html

祝大家玩得开心!ˇˍˇ

简放,杭州

相关文章:

Java异常体系结构核心解析-Throwable

资料不在于多&#xff0c;而在于精。好资料、好书&#xff0c;我们站在巨人的肩膀上前行&#xff0c;可以少走很多弯路。 通过搜索引擎找到自己需要的最好最权威信息&#xff0c;是一种很重要的能力。 Java源代码和官方资料Java™ Tutorials Java异常体系结构&#xff0c;是一种…...

Android MediaRecorder 相关

Android MediaRecorder 相关 本篇文章主要介绍下MediaRecorder类. 1:创建对象 MediaRecorder mediaRecordernew MediaRecorder();MediaRecorder可以直接通过无参构造直接创建对象. 2: 音频源 通过调用setAudioSource(int audio_source)来设置音频源,可以是麦克风.音频文件…...

Spring中关于事务的一些方方面面

事务隔离级别&#xff1a; 先了解一些事务隔离级别有哪些&#xff1a; 未提交读(Read Uncommitted)&#xff1a; 允许脏读&#xff0c;也就是可能读取到其他会话中未提交事务修改的数据 提交读(Read Committed)&#xff1a; 只能读取到已经提交的数据。Oracle等多数数据库默…...

LiveQing视频点播流媒体RTMP推流服务功能-支持配置开启 HTTPS 服务什么时候需要开启HTTPS服务

LiveQing视频点播流媒体RTMP推流服务功能支持配置开启 HTTPS 服务什么时候需要开启HTTPS服务 1、配置开启HTTPS1.1、准备https证书1.1.1、选择Nginx类型证书下载 1.2、配置 开启 HTTPS1.2.1 web页面配置1.2.2 配置文件配置 2、验证HTTPS服务3、为什么要开启HTTPS3.1、安全性要求…...

LabVIEW串口通信的激光器模块智能控制

LabVIEW串口通信的激光器模块智能控制 介绍了通过于LabVIEW的VISA串口通信技术在激光器模块控制中的应用。通过研究VISA串口通信的方法和流程&#xff0c;实现了对激光器模块的有效控制&#xff0c;解决了数据发送格式的匹配问题&#xff0c;为激光器模块的智能控制提供了一种…...

全球最受欢迎的DAWFL Studio 21.2.3.4004 中文破解版强悍来袭

1997年是一个「古老」的年代&#xff0c;那时人们还在用「猫」上网&#xff0c;微信、QQ的江湖被ICQ统治&#xff0c;音乐编辑领域 Cool Edit 如日中天。这一年 &#xff0c;FL Studio 的前身 FruityLoops 在比利时问世&#xff0c;26年来&#xff0c;FL已成长为全球最受欢迎的…...

【uni-app】常用组件和 API

常用组件 uni-app 为开发者提供了一系列基础组件&#xff0c;类似 HTML 里的基础标签元素&#xff0c;但 uni-app 的组件与 HTML 不同&#xff0c;而是与小程序相同&#xff0c;更适合手机端使用。 虽然不推荐使用 HTML 标签&#xff0c;但实际上如果开发者写了div等标签&…...

基于springboot+vue的安康旅游网站(前后端分离)

博主主页&#xff1a;猫头鹰源码 博主简介&#xff1a;Java领域优质创作者、CSDN博客专家、阿里云专家博主、公司架构师、全网粉丝5万、专注Java技术领域和毕业设计项目实战&#xff0c;欢迎高校老师\讲师\同行交流合作 ​主要内容&#xff1a;毕业设计(Javaweb项目|小程序|Pyt…...

monaco脚本编辑器 在无界中使用 鼠标点击不到

背景A项目使用无界引入B项目 经排查&#xff0c;是B使用的的monaco脚本编辑器无法在A的无界框架中引入的问题。 经查询&#xff0c;需要修改monaco源码来使之能在无界中使用。 解决方案&#xff1a;https://github.com/Tencent/wujie/issues/205。 有三种解决方案&#xff1a; …...

react中修改state中的值无效?

// 初始化state state {personArr:[{name:张三,id:1},{name:李四,id:2},{name:王五,id:3}] }componentDidMount(){const newName 赵六const indexUpdate 1const newArr this.state.personArr.map((item,index)>{if(indexUpdate index){return {...item,name:newName}}e…...

在Node.js中如何实现用户身份验证和授权

当涉及到构建安全的应用程序时&#xff0c;用户身份验证和授权是至关重要的一环。在Node.js中&#xff0c;我们可以利用一些流行的库和技术来实现这些功能&#xff0c;确保我们的应用程序具有所需的安全性。本篇博客将介绍如何在Node.js中实现用户身份验证和授权。 用户身份验…...

QT day2 2.21

1.使用手动连接&#xff0c;将登录框中的取消按钮使用qt4版本的连接到自定义的槽函数中&#xff0c;在自定义的槽函数中调用关闭函数 代码&#xff1a; #include "mywidget.h" #include "ui_mywidget.h"MyWidget::MyWidget(QWidget *parent): QWidget(pa…...

说说设备像素、css像素、设备独立像素、dpr、ppi 之间的区别

文章目录 一、背景二、介绍CSS像素设备像素设备独立像素dprppi 三、总结参考文献 一、背景 在css中我们通常使用px作为单位&#xff0c;在PC浏览器中css的1个像素都是对应着电脑屏幕的1个物理像素 这会造成一种错觉&#xff0c;我们会认为css中的像素就是设备的物理像素 但实…...

文生视频Sora

Sora&#xff1a;scalable diffusion models with transformers 文生视频模型 Sora&#xff1a;视频生成模型60s&#xff0c;视频中体现一定的物理逻辑 时空patch&#xff0c;是Sora创新的核心。 Sora 到底是不是物理引擎甚至世界模型&#xff1f;数据驱动的物理引擎 帆船在水里…...

汽车常识网:电脑主机如何算功率的计算方法?

今天汽车知识网就给大家讲解一下如何计算一台主机的功率。 它还会解释如何计算计算机主机所需的功率&#xff1f; &#xff1f; &#xff08;如何计算电脑主机所需的功率&#xff09;进行说明。 如果它恰好解决了您现在面临的问题&#xff0c;请不要忘记关注本站。 让我们现在就…...

c语言常见操作符及操作符优先级

目录 概述1. 算术操作符&#xff1a;2. 关系操作符&#xff1a;3. 逻辑操作符&#xff1a;4. 位操作符&#xff1a; *常见操作符优先级* 概述 C语言中有多种操作符&#xff0c;用于执行不同的操作。下面是一些常见的C语言操作符以及示例代码&#xff1a; 1. 算术操作符&…...

IO进程线程:通信

1.定义互斥锁 #include<myhead.h>int num520;//临界资源//1.创建一个互斥锁变量 pthread_mutex_t mutex;//定义任务&#xff11;函数 void *task1(void *arg) {printf("11111111111111\n");//3.获取锁资源pthread_mutex_lock(&mutex);num1314;sleep(3);pr…...

神经网络系列---常用梯度下降算法

文章目录 常用梯度下降算法随机梯度下降&#xff08;Stochastic Gradient Descent&#xff0c;SGD&#xff09;&#xff1a;随机梯度下降数学公式&#xff1a;代码演示 批量梯度下降&#xff08;Batch Gradient Descent&#xff09;批量梯度下降数学公式&#xff1a;代码演示 小…...

Flink 的历史版本特性介绍(一)

如果你还不了解 Flink 是什么,可以查看我之前的介绍文章:Flink 介绍 如果你想跟着我一起学习 flink,欢迎查看订阅专栏:Flink 专栏 这篇文章列举了 Flink 每次发布的版本中的重要特性,从中可以看出 Flink 是如何一步一步发展到今天的。 Flink 的前身是 Stratosphere 项目…...

【尚硅谷】MybatisPlus 学习笔记(下)

目录 六、插件 6.1、分页插件 6.1.1、添加配置类 6.1.2、测试 6.2、xml自定义分页 6.2.1、UserMapper中定义接口方法 6.2.2、UserMapper.xml中编写SQL 6.2.3、测试 6.3、乐观锁 6.3.1、场景 6.3.2、乐观锁与悲观锁 6.3.3、模拟修改冲突 数据库中增加商品表 添加数…...

零门槛NAS搭建:WinNAS如何让普通电脑秒变私有云?

一、核心优势&#xff1a;专为Windows用户设计的极简NAS WinNAS由深圳耘想存储科技开发&#xff0c;是一款收费低廉但功能全面的Windows NAS工具&#xff0c;主打“无学习成本部署” 。与其他NAS软件相比&#xff0c;其优势在于&#xff1a; 无需硬件改造&#xff1a;将任意W…...

《Qt C++ 与 OpenCV:解锁视频播放程序设计的奥秘》

引言:探索视频播放程序设计之旅 在当今数字化时代,多媒体应用已渗透到我们生活的方方面面,从日常的视频娱乐到专业的视频监控、视频会议系统,视频播放程序作为多媒体应用的核心组成部分,扮演着至关重要的角色。无论是在个人电脑、移动设备还是智能电视等平台上,用户都期望…...

智能在线客服平台:数字化时代企业连接用户的 AI 中枢

随着互联网技术的飞速发展&#xff0c;消费者期望能够随时随地与企业进行交流。在线客服平台作为连接企业与客户的重要桥梁&#xff0c;不仅优化了客户体验&#xff0c;还提升了企业的服务效率和市场竞争力。本文将探讨在线客服平台的重要性、技术进展、实际应用&#xff0c;并…...

CocosCreator 之 JavaScript/TypeScript和Java的相互交互

引擎版本&#xff1a; 3.8.1 语言&#xff1a; JavaScript/TypeScript、C、Java 环境&#xff1a;Window 参考&#xff1a;Java原生反射机制 您好&#xff0c;我是鹤九日&#xff01; 回顾 在上篇文章中&#xff1a;CocosCreator Android项目接入UnityAds 广告SDK。 我们简单讲…...

ServerTrust 并非唯一

NSURLAuthenticationMethodServerTrust 只是 authenticationMethod 的冰山一角 要理解 NSURLAuthenticationMethodServerTrust, 首先要明白它只是 authenticationMethod 的选项之一, 并非唯一 1 先厘清概念 点说明authenticationMethodURLAuthenticationChallenge.protectionS…...

精益数据分析(97/126):邮件营销与用户参与度的关键指标优化指南

精益数据分析&#xff08;97/126&#xff09;&#xff1a;邮件营销与用户参与度的关键指标优化指南 在数字化营销时代&#xff0c;邮件列表效度、用户参与度和网站性能等指标往往决定着创业公司的增长成败。今天&#xff0c;我们将深入解析邮件打开率、网站可用性、页面参与时…...

.Net Framework 4/C# 关键字(非常用,持续更新...)

一、is 关键字 is 关键字用于检查对象是否于给定类型兼容,如果兼容将返回 true,如果不兼容则返回 false,在进行类型转换前,可以先使用 is 关键字判断对象是否与指定类型兼容,如果兼容才进行转换,这样的转换是安全的。 例如有:首先创建一个字符串对象,然后将字符串对象隐…...

使用LangGraph和LangSmith构建多智能体人工智能系统

现在&#xff0c;通过组合几个较小的子智能体来创建一个强大的人工智能智能体正成为一种趋势。但这也带来了一些挑战&#xff0c;比如减少幻觉、管理对话流程、在测试期间留意智能体的工作方式、允许人工介入以及评估其性能。你需要进行大量的反复试验。 在这篇博客〔原作者&a…...

现有的 Redis 分布式锁库(如 Redisson)提供了哪些便利?

现有的 Redis 分布式锁库&#xff08;如 Redisson&#xff09;相比于开发者自己基于 Redis 命令&#xff08;如 SETNX, EXPIRE, DEL&#xff09;手动实现分布式锁&#xff0c;提供了巨大的便利性和健壮性。主要体现在以下几个方面&#xff1a; 原子性保证 (Atomicity)&#xff…...

DingDing机器人群消息推送

文章目录 1 新建机器人2 API文档说明3 代码编写 1 新建机器人 点击群设置 下滑到群管理的机器人&#xff0c;点击进入 添加机器人 选择自定义Webhook服务 点击添加 设置安全设置&#xff0c;详见说明文档 成功后&#xff0c;记录Webhook 2 API文档说明 点击设置说明 查看自…...