运行时异常自动抛出去了(throws RuntimeException), 编译时异常要手动抛(throw exception)
    image.png

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