亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 337 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號