public class MainException {
public static void main(String[] args) {
int a = 0;
int b = 10;
try {
test(0, 10);
} catch (ArithmeticException e) {
System.out.println("程序出现错误");
} finally {
System.out.println("程序执行完成");
}
}
public static int test(int a, int b) throws ArithmeticException {
return b / a;
}
}
public class MainException {
public static void main(String[] args) throws Exception {
int a = 0;
int b = 10;
if (a == 0) {
throw new MyAthException("a不可等于0,此处中断程序的运行");
} else {
System.out.println(b / a);
}
}
}
//此处为我们自定义的异常
class MyAthException extends Exception {
public MyAthException(String message) {
super(message);
}
}