Java try/catch 块中的编译类问题

我在 JAVA 代码中有 try 和 catch 块


import java.io.FileOutputStream;

import java.util.zip.ZipOutputStream;


public class TryTest {


    public static void main(String[] args) {

        String zipPath ="D:/test";

        try (ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipPath))){

            String Hello ="Hello";

            System.out.println("==============>"+Hello);

        }catch (Exception e) {

            e.printStackTrace();

        }


    }


}

我编译的类看起来像


/* * 使用 CFR 0.145 反编译。*/ ....


try {

    try (ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(string));){

        String string2 = "Hello";

        System.out.println("==============>" + string2);

    }

....


我很奇怪为什么在编译时添加了另一个 try 块。


完整源代码在


https://github.com/vikram06/java_try_catch_bug


qq_笑_17
浏览 247回答 3
3回答

三国纷争

这在 JLS 14.20.3.2 Extended try-with-resources中有解释:扩展的 try-with-resources 语句的含义:try ResourceSpecification     Block Catchesopt Finallyopt由嵌套在 try-catch 或 try-finally 或 try-catch-finally 语句中的基本 try-with-resources 语句 (§14.20.3.1) 的以下翻译给出:try {    try ResourceSpecification         Block } Catchesopt Finallyopt翻译的效果是将 ResourceSpecification 放在 try 语句“内部”。这允许扩展的 try-with-resources 语句的 catch 子句捕获由于任何资源的自动初始化或关闭而引起的异常。此外,在执行 finally 块时,所有资源都已关闭(或试图关闭),这与 finally 关键字的意图保持一致。

大话西游666

当您使用 try with resources(我的意思是try (...) {...)时,Java 编译器会生成额外的代码部分来显示来自 type 的局部变量的堆栈跟踪Throwable。这是因为 Java 编译器正在将 try with resources 语句分解为单独的尝试 - 一个用于关闭资源,另一个用于try.反编译后如何显示 - 这取决于您使用的反编译器。

白衣非少年

对于它的价值 -实际字节码与输入的相似性要小得多 - 尝试将 cfr 与参数一起使用--tryresources false --decodefinally false你得到了未加糖的代码,它更接近实际的字节码。    public static void main(String[] args) {    String zipPath = "D:/test";    try {        ZipOutputStream zipOut;        block11 : {            zipOut = new ZipOutputStream(new FileOutputStream(zipPath));            Throwable throwable = null;            try {                String Hello2332 = "Hello";                System.out.println("==============>" + Hello2332);                if (zipOut == null) return;                if (throwable == null) break block11;            }            catch (Throwable Hello2332) {                try {                    throwable = Hello2332;                    throw Hello2332;                }                catch (Throwable throwable2) {                    if (zipOut == null) throw throwable2;                    if (throwable == null) {                        zipOut.close();                        throw throwable2;                    }                    try {                        zipOut.close();                        throw throwable2;                    }                    catch (Throwable throwable3) {                        throwable.addSuppressed(throwable3);                        throw throwable2;                    }                }            }            try {                zipOut.close();                return;            }            catch (Throwable Hello2332) {                throwable.addSuppressed(Hello2332);                return;            }        }        zipOut.close();        return;    }    catch (Exception e) {        e.printStackTrace();    }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java