为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

qq_笑_17 2023-05-10 14:05:26
我在 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
查看完整描述

3 回答

?
三国纷争

TA贡献1804条经验 获得超7个赞

这在 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 关键字的意图保持一致。


查看完整回答
反对 回复 2023-05-10
?
大话西游666

TA贡献1817条经验 获得超14个赞

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

反编译后如何显示 - 这取决于您使用的反编译器。


查看完整回答
反对 回复 2023-05-10
?
白衣非少年

TA贡献1155条经验 获得超0个赞

对于它的价值 -实际字节码与输入的相似性要小得多 - 尝试将 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();

    }

}


查看完整回答
反对 回复 2023-05-10
  • 3 回答
  • 0 关注
  • 243 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信