如何在 Java 中捕獲除以零異常?
當您將一個數字除以零時,將會丟擲異常數字。
示例
public class DividedByZero { public static void main(String args[]) { int a, b; try { a = 0; b = 54/a; System.out.println("hello"); } catch (ArithmeticException e) { System.out.println("you cannot divide a number with zero"); } } }
輸出
you cannot divide a number with zero
廣告