运行时异常自动抛出去了(throws RuntimeException), 编译时异常要手动抛(throw exception)
package com.itheima.d5_exception_handle_runtime;
/**
* 目标: 运行时异常的处理机制
*
* 可以不处理 编译阶段又不报错
* 按照理论规则:建议还是处理,只需要在最外层捕获处理即可
*/
public class Test {
public static void main(String[] args) {
System.out.println("程序开始。。。。");
try {
chu(10,0); // 正常会报错,除数不能为0,使用try方法
} catch (Exception e) {
e.printStackTrace(); // 这种方法不会立刻结束程序,(因为不会让JVM虚拟机接收异常对象,所以不会终止程序)
}
System.out.println("程序结束"); // 这条代码还好运行
}
private static void chu(int a, int b) { //throws RuntimeException 直接默认会抛出来,因为余数为0
System.out.println(a);
System.out.println(b);
int c = a / b;
System.out.println(c);
}
}