天天看點

Java異常被抛出或被捕獲之後,代碼是否繼續執行的問題

在寫程式的時候,我們經常被教導,要對異常的資訊進行處理,哪裡該抛出異常。但是,更多的時候,我們隻是模仿異常的抛出,卻不知道為什麼要這樣抛異常(被catch了?被向上抛了?後面的代碼是否執行了?)。

接下來,我就簡單的說一下異常抛出後的代碼執行問題。此處不讨論自定義異常,因為自定義異常有自己的處理方式。

一、結論:

  1. 凡是有異常的地方,需要有處理異常的地方。(示例:Demo1, Demo2)
  2. 隻要異常被處理,異常處理之後的代碼都可以正常執行。(示例:Demo1, Demo2)
  3. 異常被往上抛出,則抛出異常之後的代碼将不被執行。(示例:Demo2, Demo3)

二、示例代碼

接下來用兩段代碼來說明異常抛出後代碼執行的順序

示例1.

Demo1.java

/**
 * 抛出異常的代碼是放在 try 中
 */
public class Demo1 {
    public static void main(String[] args) {
        try {
            print();
            Thread.sleep(200);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("列印抛出異常");
        }
        System.out.println("程式結束");
    }

    private static void print() {
        int index = 0;
        while (index < 15) {
            try {
                Thread.sleep(200);
                ++index;
                if (index == 5 || index == 10) {
                    throw new Exception();
                }
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("循環抛出異常");
            }
            System.out.println("index = " + index);
        }
        System.out.println("循環結束");
    }
}           

運作結果:

index = 1
index = 2
index = 3
index = 4
java.lang.Exception
循環抛出異常
index = 5
    at com.example.demo.Demo1.print(Demo1.java:22)
    at com.example.demo.Demo1.main(Demo1.java:6)
index = 6
index = 7
index = 8
index = 9
java.lang.Exception
    at com.example.demo.Demo1.print(Demo1.java:22)
    at com.example.demo.Demo1.main(Demo1.java:6)
循環抛出異常
index = 10
index = 11
index = 12
index = 13
index = 14
index = 15
循環結束
程式結束           

示例2.

Demo2.java

/**
 * 抛出異常的代碼是放在 try 外
 */
public class Demo2 {
    public static void main(String[] args) {
        try {
            print();
            Thread.sleep(200);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("列印抛出異常");
        }
        System.out.println("程式結束");
    }

    private static void print() throws Exception{
        int index = 0;
        while (index < 15){
            if (index == 5 || index == 10){
                throw new Exception();
            }
            try {
                Thread.sleep(200);
                ++index;
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("循環抛出異常");
            }
            System.out.println("index = "+index);
        }
        System.out.println("循環結束");
    }
}           

運作結果

index = 1
index = 2
index = 3
index = 4
index = 5
java.lang.Exception
    at com.example.demo.Demo2.print(Demo2.java:19)
    at com.example.demo.Demo2.main(Demo2.java:6)
列印抛出異常
程式結束
           

示例3.

Demo3.java

/**
 * 不對異常進行捕獲,隻是往上抛
 */
public class Demo3 {
    public static void main(String[] args) throws Exception {
        print();
        System.out.println("程式結束");
    }

    private static void print() throws Exception {
        int index = 0;
        while (index < 15){
            ++index;
            if (index == 5 || index == 10){
                throw new Exception();
            }
            System.out.println("index = "+index);
        }
        System.out.println("循環結束");
    }
}
           
index = 1
index = 2
index = 3
index = 4
Exception in thread "main" java.lang.Exception
    at com.example.demo.Demo3.print(Demo3.java:15)
    at com.example.demo.Demo3.main(Demo3.java:5)           

三、分析

Demo1 與 Demo2 的差別在于抛出異常的代碼是放在 try 中,還是放在 try 外。

//抛出異常的代碼
if (index == 5 || index == 10){
    throw new Exception();
}           

分析:Demo1

  1. print 方法沒有往 main 方法抛出異常,而是在循環中直接 catch 異常。
  2. 異常被 catch 之後,循環繼續執行。
  3. 在 print 方法執行結束之後,因為 main 方法沒有出現任何異常,print 方法之後的代碼都能正常執行。

分析:Demo2

  1. print 方法往 main 方法抛出異常。
  2. 循環在異常出現的時候,循環将不再執行,異常被抛出到 main 方法中。
  3. main 方法 catch 異常,異常在 catch 被處理之後,catch 之後的代碼都能正常執行。

分析:Demo3

  1. print 方法往 main 方法抛出異常。循環在異常出現的時候,循環将不再執行,異常被抛出到 main 方法中。
  2. main 方法往上繼續抛出異常。在 print 出現異常的時候,print 之後的代碼将不再執行。

如果文章有幫助到了你,歡迎點贊、轉發。

如果文章有錯誤的地方,歡迎留言交流。