編寫應用程序,從鍵盤輸入兩個數作爲除數和被除數。要求程序中捕獲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("程序執行結束");
}
}
}