【Java】 NullPointerException、ArrayIndexOutOfBoundsException、ClassCastException、ArrayIndexOutOfBoundsException、ArrayStoreException、ArithmeticException等没有异常堆栈信息

今天工作中,临时Fix一个bug,一看日志“java.lang.ClassCastException: null” 相当懵逼,没有详细堆栈信息,这咋整。虽然根据上下文可以推测问题代码的大致位置,但不敢拍板啊。只好google找一下,在Stackoverflow上果然有解决办法
【解决方法】
在java启动命令中添加“-XX:-OmitStackTraceInFastThrow”即可输出详细堆栈信息——亲测可用。
【问题原因】
JVM(HotSpot JVM)进行了优化。当第一次发生异常(通常为NullPointerException)时,将打印完整的堆栈跟踪,并且JVM会记住堆栈跟踪(或者可能只是代码的位置)。 当该异常经常发生时,将不再打印堆栈跟踪,这既可以实现更好的性能,【CoederBaby】又不会使相同的堆栈跟踪充满日志
【进一步分析】
参看JVM源码(参见附录2),可见这个优化同时试用于以下异常:
- NullPointerException
- ArrayIndexOutOfBoundsException
- ClassCastException
- ArrayIndexOutOfBoundsException
- ArrayStoreException
- ArithmeticException
相关核心代码片段:
// If this throw happens frequently, an uncommon trap might cause
// a performance pothole. If there is a local exception handler,
// and if this particular bytecode appears to be deoptimizing often,
// let us handle the throw inline, with a preconstructed instance.
// Note: If the deopt count has blown up, the uncommon trap
// runtime is going to flush this nmethod, not matter what.
if (treat_throw_as_hot
&& (!StackTraceInThrowable || OmitStackTraceInFastThrow)) {
// If the throw is local, we use a pre-existing instance and
// punt on the backtrace. This would lead to a missing backtrace
// (a repeat of 4292742) if the backtrace object is ever asked
// for its backtrace.
// Fixing this remaining case of 4292742 requires some flavor of
// escape analysis. Leave that for the future.
ciInstance* ex_obj = NULL;
switch (reason) {
case Deoptimization::Reason_null_check:
ex_obj = env()->NullPointerException_instance();
break;
case Deoptimization::Reason_div0_check:
ex_obj = env()->ArithmeticException_instance();
break;
case Deoptimization::Reason_range_check:
ex_obj = env()->ArrayIndexOutOfBoundsException_instance();
break;
case Deoptimization::Reason_class_check:
if (java_bc() == Bytecodes::_aastore) {
ex_obj = env()->ArrayStoreException_instance();
} else {
ex_obj = env()->ClassCastException_instance();
}
break;
default:
break;
}
参考:
- stackoverflow : https://stackoverflow.com/questions/2411487/nullpointerexception-in-java-with-no-stacktrace
- 相关JVM源码:https://hg.openjdk.java.net/jdk/jdk/file/tip/src/hotspot/share/opto/graphKit.cpp
【Java】 NullPointerException、ArrayIndexOutOfBoundsException、ClassCastException、ArrayIndexOutOfBoundsException、ArrayStoreException、ArithmeticException等没有异常堆栈信息的更多相关文章
- [Java]借助PrintWriter类和StringWriter类,取出异常堆栈信息放入字符串中
在程序开发中,有时我们不仅需要将异常堆栈信息打印在控制台里或是log里,可能还需要将它存在String中,再送到合适的地方,如错误页面,数据库等. 要取异常堆栈信息,具体的函数就是: /** * Ge ...
- 打印Java异常堆栈信息
背景 在开发Java应用程序的时候,遇到程序抛异常,我们通常会把抛异常时的运行时环境保存下来(写到日志文件或者在控制台中打印出来).这样方便后续定位问题. 需要记录的运行时环境包含两部分内容:抛异常时 ...
- Java获取异常堆栈信息
方法一: public static String getStackTrace(Throwable t) { StringWriter sw = new StringWriter(); PrintWr ...
- Java异常---获取异常的堆栈信息
Java 实例 - 获取异常的堆栈信息 Java 实例 以下实例演示了使用异常类的 printStack() 方法来获取堆栈信息: Main.java 文件 public class Main{ p ...
- 别样JAVA学习(六)继承下(2.3)异常下
1.RuntimeException Exception中有一个特殊的子类异常RuntimeException执行时异常. 假设在函数内容抛出该异常,函数上能够不用声明.编译一样通过. 假设在函数上声 ...
- Win下,通过Jstack截取Java进程中的堆栈信息
在Java软件的使用过程中,有时会莫名的出现奇怪的问题.而这些问题常常无法使用日志信息定位,这时我们就需要通过查看进程内部线程的堆栈调用关系来分析问题出在哪里. 举个例子,当我们在做某个操作时,莫名的 ...
- 【POI xls Java map】使用POI处理xls 抽取出异常信息 --java1.8Group by ---map迭代 -- 设置单元格高度
代码处理逻辑: 代码流程: 1.首先需要创建一个实体 用来存储 相关信息 package com.sxd.test.unusualName; public class NameEntity { pri ...
- java.lang.UnsupportedClassVersionError: Bad version number in .class file异常
java.lang.UnsupportedClassVersionError: Bad version number in .class file异常 部署工程时也出现过因为版本不同引起的问题,那时我 ...
- java.net.SocketException:Software caused connection abort: recv failed 异常分析 +socket客户端&服务端代码
java.net.SocketException:Software caused connection abort: recv failed 异常分析 分类: 很多的技术 2012-01-04 12: ...
随机推荐
- 每日背单词 - Jun.
6月1日裸辞,计划休息到端午节后,这段时间玩的确实很开心,每天和朋友一起吹灯拔蜡:好不自在,可惜假期马上结束了,从今天开始恢复学习状态. 2018年6月1日 - 2018年6月14日 辞职休假 201 ...
- Mongo Delete-19
数据初始化 db.inventory.insertMany( [ { item: "journal", qty: 25, size: { h: 14, w: 21, uom: &q ...
- 2、【Spark】Spark环境搭建(集群方式)
Spark集群方式搭建结构如图所示,按照主从方式.
- Haproxy 使用block 阻止域名访问到某个子目录报403
配置教程如下: acl is_https_com hdr_beg(host) www.baidu.com #定义规则域名 acl api_block_url_web url_dir -i /web/ ...
- Python学习笔记--threading线程
通过线程来实现多任务并发.提高性能.先看看例子. #!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2020-03-02 21:10:39 ...
- python settings 中通过字符串导入模块
1. 项目文件结构 set_test ├─ main.py # 入口函数 │ ├─notify # 自定义的模块 │ ├─ email.py # 自定义模块 │ ├─ msg.py # 自定义模块 │ ...
- 7-34 jmu-python-是否偶数 (10 分)
输入一个整数,判断是否偶数 输入样例: 7 输出样例: 7不是偶数 输入样例: 8 输出样例: 8是偶数 a = int(input()) if (a % 2 == 0): print('%d是偶数' ...
- Sketchup二次开发教程
Sketchup提供了两套API: C API,主要用于读写SU文件.我们的SU文件导入功能就是用这套API做的 Ruby API,用于开发SU插件 这次我们主要关注Ruby API,因为它是实现更丰 ...
- 基于sklearn的metrics库的常用有监督模型评估指标学习
一.分类评估指标 准确率(最直白的指标)缺点:受采样影响极大,比如100个样本中有99个为正例,所以即使模型很无脑地预测全部样本为正例,依然有99%的正确率适用范围:二分类(准确率):二分类.多分类( ...
- python之模块中包的介绍
跨文件夹导入模块 1:有文件夹a,名下有ma功能,在文件夹外调用ma功能的话, 导入import a.ma 运用ma() 或者 from a import ma ma() 2;假定a有多重文件夹,想要 ...