Java Exception例外メカニズム(3)--try-catch-finallyブロックでfinallyブロックが唯一実行されない場合は何ですか?

4007 ワード

Lalala、前に面接問題を見たことがあります.try-catch-finallyブロックの中で、finallyブロックが唯一実行されない状況は何ですか.仏系ブロガーとして、最初は、この命題に偽善的な批判を与えることが唯一とは何か、唯一とは何か、唯一という字は私の目には私のIQに対する冒解一:try前異常、システムエラー、もちろんfinallyは実行されません
public class Math {
    public int  method01(int i,int j){//    
    int t =1/0;  // try    ,    
        System.out.println("mehod    ");
        int c=0;
    try {
         c =i/j;
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("fdfd");

        }finally {

            System.out.println("finally");
            return c;
        }

解二:tryまたはcatchブロックでexit()がある場合、プログラムを早期に終了させる
public class Math {
    public int  method01(int i,int j){//    

        System.out.println("mehod    ");
        int c=0;
    try {
         c =i/j;
         System.exit(0);  //    
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("cathc     ");

        }finally {

            System.out.println("finally     ");
            return c;
        }               
        }   
    }

public class TrowsDamo2 {
  public static void main(String[] args) throws Exception  {
        Math math = new Math();
        System.out.println("    ");
int div2 = math.method01(15, 3);//    Math  method01    
        System.out.println("    ");
        }
}

出力結果
    
mehod      

引用:mathを呼び出すと.method 01(int i,int j)メソッドの場合、jが入力する値は0であり、プログラムはMathクラスに実行され、tryブロックの最初の行c=i/jである.すると、異常が発生する、すなわちcatchブロック処理に移行し、その後のSystemとなる.exit(0);実行されません.finallyブロックは次のように実行されます.
    
mehod    
cathc     
finally     
    

もし以上の内容があなたを助けることができるならば、きっとあなたのかわいい小指を動かして、いいね、転載して、評論して、私に1セットの大きい保健を手伝います~~~敬上!