Try Error


Submit solution

Points: 50
Time limit: 1.0s
Memory limit: 256M

Authors:
Problem types
Allowed languages
Java 19, Java 8

題目說明

請撰寫一段程式碼,創建一個包含 5 個元素的字串陣列names,並嘗試訪問超出陣列範圍的索引位置,捕捉並處理ArrayIndexOutOfBoundsException

try區塊中,迴圈訪問每個索引並打印出該元素,
當發生例外狀況時,在catch區塊中打印出錯誤訊息"Error: Index is out of bounds."和exception的錯誤訊息(提示:使用e.getMessage())

並在程式結尾打印"Program continues after exception handling."

使用以下程式碼作答:
public class MyProgram {
    public static void main(String[] args) {
        String[] names = {"Alice", "Bob", "Charlie", "David", "Eve"};

        try {
            // 嘗試訪問超出陣列範圍的索引位置
            /*
                在此作答
            */
        } catch (ArrayIndexOutOfBoundsException e) {
            // 捕捉並處理 ArrayIndexOutOfBoundsException
            /*
                在此作答
            */
        }

        System.out.println("Program continues after exception handling.");
    }
}

輸入

這題不用輸入

輸出

依主程式執行輸出

測試資料輸入

這題不用輸入

測試資料輸出

Alice
Bob
Charlie
David
Eve
Error: Index is out of bounds.
Index 5 out of bounds for length 5
Program continues after exception handling.

Comments

There are no comments at the moment.