Exception 异常类
- 异常的描述
- 异常的分类
- 异常的继承体系
package jericho.demo.exception;
public class Demo1_Exception {
public static void main(String[] args) {
try {
int x = div(10,0); // 这里抛出除零异常
System.out.println(x);
}catch (ArithmeticException | Exception e){
System.out.println("除数为零");
}
//...通过trycatch处理的异常,程序不会停止
}
public static int div(int a, int b) {
return a / b;
}
}
- 运行时异常抛出
package jericho.demo.exception;
public class Demo1_Exception {
public static void main(String[] args) {
try {
div(1);
} catch (Exception e) {
System.out.println(e);
}
//...通过trycatch处理的异常,程序不会停止
}
public static void div(int a) {
if (a > 0) {
throw new RuntimeException("大于零");
}
}
}