異常類練習——編寫應用程序,從鍵盤輸入兩個數作爲除數和被除數。捕獲NumberFormatException 異常和ArithmeticException 異常,

編寫應用程序,從鍵盤輸入兩個數作爲除數和被除數。要求程序中捕獲NumberFormatException 異常和ArithmeticException 異常,而且無論在哪種情況下,“程序執行結束”這句話都會在控制檯輸出。

package text;

import java.util.Scanner;

public class Text_8 {
		public static void main(String[] args) {
			Scanner sc = new Scanner(System.in);
			try {
				System.out.print("輸入兩個整數:");
			int a= sc.nextInt();
			int b = sc.nextInt();
			System.out.println(a/b);
			}catch(NumberFormatException e) {
				System.out.println("發生異常");
			}catch(ArithmeticException ee) {
				System.out.println("發現了異常");
			}finally {
				System.out.println("程序執行結束");
			}	
		}
}

在這裏插入圖片描述
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章