如何在 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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
JavaScript
PHP