深入拆解 Java 虚拟机 】Exception异常笔记

2024-09-07 09:32

本文主要是介绍深入拆解 Java 虚拟机 】Exception异常笔记,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

【深入拆解 Java 虚拟机 】Exception异常笔记

  • try-with-resource语法糖
  • finally

try-with-resource语法糖

try后对象的close方法都会被运行。

package com.exception.demo;
public class Foo implements AutoCloseable {private final String name;public Foo(String name) { this.name = name; }@Overridepublic void close() {System.out.println(name);}public static void main(String[] args) {try (Foo foo0 = new Foo("Foo0"); // try-with-resourcesFoo foo1 = new Foo("Foo1");Foo foo2 = new Foo("Foo2")) {System.out.println("ssssss");try {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("22222");throw new RuntimeException("Initial");}}
}
start
end
Foo2
Foo1
Foo0
Exception in thread "main" java.lang.RuntimeException: Initialat com.exception.demo.Foo.main(Foo.java:22)

finally


public class Foo2 {private int tryBlock;private int catchBlock;private int finallyBlock;private int methodExit;public void test() {for (int i = 0; i < 100; i++) {try {tryBlock = 0;if (i < 50) {continue;} else if (i < 80) {break;} else {return;}} catch (Exception e) {catchBlock = 1;} finally {finallyBlock = 2;}}methodExit = 3;}
}

对应生成的字节码为

public class com.exception.demo.Foo2 {public com.exception.demo.Foo2();Code:0: aload_01: invokespecial #1                  // Method java/lang/Object."<init>":()V4: returnpublic void test();Code:0: iconst_01: istore_12: iload_13: bipush        1005: if_icmpge     758: aload_09: iconst_010: putfield      #2                  // Field tryBlock:I13: iload_114: bipush        5016: if_icmpge     2719: aload_020: iconst_221: putfield      #3                  // Field finallyBlock:I24: goto          6927: iload_128: bipush        8030: if_icmpge     4133: aload_034: iconst_235: putfield      #3                  // Field finallyBlock:I38: goto          7541: aload_042: iconst_243: putfield      #3                  // Field finallyBlock:I46: return47: astore_248: aload_049: iconst_150: putfield      #5                  // Field catchBlock:I53: aload_054: iconst_255: putfield      #3                  // Field finallyBlock:I58: goto          6961: astore_362: aload_063: iconst_264: putfield      #3                  // Field finallyBlock:I67: aload_368: athrow69: iinc          1, 172: goto          275: aload_076: iconst_377: putfield      #6                  // Field methodExit:I80: returnException table:from    to  target type8    19    47   Class java/lang/Exception27    33    47   Class java/lang/Exception8    19    61   any27    33    61   any47    53    61   any61    62    61   any
}

finally在try、catch后都会运行一次,如果内部还有if等多个分支,那么还会赋值运行多次。

这篇关于深入拆解 Java 虚拟机 】Exception异常笔记的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1144689

相关文章

Spring Cloud LoadBalancer 负载均衡详解

《SpringCloudLoadBalancer负载均衡详解》本文介绍了如何在SpringCloud中使用SpringCloudLoadBalancer实现客户端负载均衡,并详细讲解了轮询策略和... 目录1. 在 idea 上运行多个服务2. 问题引入3. 负载均衡4. Spring Cloud Load

Springboot中分析SQL性能的两种方式详解

《Springboot中分析SQL性能的两种方式详解》文章介绍了SQL性能分析的两种方式:MyBatis-Plus性能分析插件和p6spy框架,MyBatis-Plus插件配置简单,适用于开发和测试环... 目录SQL性能分析的两种方式:功能介绍实现方式:实现步骤:SQL性能分析的两种方式:功能介绍记录

在 Spring Boot 中使用 @Autowired和 @Bean注解的示例详解

《在SpringBoot中使用@Autowired和@Bean注解的示例详解》本文通过一个示例演示了如何在SpringBoot中使用@Autowired和@Bean注解进行依赖注入和Bean... 目录在 Spring Boot 中使用 @Autowired 和 @Bean 注解示例背景1. 定义 Stud

如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别详解

《如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别详解》:本文主要介绍如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别的相关资料,描述了如何使用海康威视设备网络SD... 目录前言开发流程问题和解决方案dll库加载不到的问题老旧版本sdk不兼容的问题关键实现流程总结前言作为

Ubuntu固定虚拟机ip地址的方法教程

《Ubuntu固定虚拟机ip地址的方法教程》本文详细介绍了如何在Ubuntu虚拟机中固定IP地址,包括检查和编辑`/etc/apt/sources.list`文件、更新网络配置文件以及使用Networ... 1、由于虚拟机网络是桥接,所以ip地址会不停地变化,接下来我们就讲述ip如何固定 2、如果apt安

SpringBoot中使用 ThreadLocal 进行多线程上下文管理及注意事项小结

《SpringBoot中使用ThreadLocal进行多线程上下文管理及注意事项小结》本文详细介绍了ThreadLocal的原理、使用场景和示例代码,并在SpringBoot中使用ThreadLo... 目录前言技术积累1.什么是 ThreadLocal2. ThreadLocal 的原理2.1 线程隔离2

springboot将lib和jar分离的操作方法

《springboot将lib和jar分离的操作方法》本文介绍了如何通过优化pom.xml配置来减小SpringBoot项目的jar包大小,主要通过使用spring-boot-maven-plugin... 遇到一个问题,就是每次maven package或者maven install后target中的ja

Java中八大包装类举例详解(通俗易懂)

《Java中八大包装类举例详解(通俗易懂)》:本文主要介绍Java中的包装类,包括它们的作用、特点、用途以及如何进行装箱和拆箱,包装类还提供了许多实用方法,如转换、获取基本类型值、比较和类型检测,... 目录一、包装类(Wrapper Class)1、简要介绍2、包装类特点3、包装类用途二、装箱和拆箱1、装

如何利用Java获取当天的开始和结束时间

《如何利用Java获取当天的开始和结束时间》:本文主要介绍如何使用Java8的LocalDate和LocalDateTime类获取指定日期的开始和结束时间,展示了如何通过这些类进行日期和时间的处... 目录前言1. Java日期时间API概述2. 获取当天的开始和结束时间代码解析运行结果3. 总结前言在J

Java深度学习库DJL实现Python的NumPy方式

《Java深度学习库DJL实现Python的NumPy方式》本文介绍了DJL库的背景和基本功能,包括NDArray的创建、数学运算、数据获取和设置等,同时,还展示了如何使用NDArray进行数据预处理... 目录1 NDArray 的背景介绍1.1 架构2 JavaDJL使用2.1 安装DJL2.2 基本操