java異常finally相關執行順序

2021-09-02 09:01:28 字數 523 閱讀 2890

package com.core;

public class testexception2

@suppresswarnings("finally")

boolean testex() throws exception catch (exception e) finally

} public static void main(string args) catch (exception e)

} }

控制台輸出:

testex, catch exception

testex, finally; return value=false

如果將testex中finally字句的return注釋掉,該方法會丟擲異常給main方法。

結論:如果catch到異常會執行catch子句,在返回return或throw之前會執行finally子句,如果finally子句存在return或throw會優先執行。

Java異常捕獲機制中的finally

package day08 finally塊 finally塊定義在異常捕獲機制的最後 可以直接跟在try塊之後或最後乙個catch塊之後。finally塊中的 一定會執行,無論try塊中的 是否丟擲異常。所以通常會把釋放資源等操作放在finally中,例如關閉流等。author kaixu pub...

Java異常處理之finally的使用總結

1.語法 完全體的異常捕獲處理機制包括try.catch.finally三個模組,語法結構如下 trycatch exception e finally2.特點 finally在使用時有以下特點。a.在異常處理時,無論是否發生異常或try catch中出現return語句,finally中的語句都會...

物件導向 異常finally

finally 塊 定義一定執行的 通常用於關閉資源。class fushuexception extends exception class demo class exceptiondemo5 catch fushuexception e finally system.out.println ov...