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 關鍵字的意圖保持一致。

TA貢獻1817條經驗 獲得超14個贊
當您使用 try with resources(我的意思是try (...) {...
)時,Java 編譯器會生成額外的代碼部分來顯示來自 type 的局部變量的堆棧跟蹤Throwable
。這是因為 Java 編譯器正在將 try with resources 語句分解為單獨的嘗試 - 一個用于關閉資源,另一個用于try
.
反編譯后如何顯示 - 這取決于您使用的反編譯器。

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();
? ? }
}
添加回答
舉報