异常

异常处理机制

  1. 抛出异常

  2. 捕获异常

  3. 异常处理的五个关键字

    1. try:监控区域

    2. catch:捕获异常

    3. finally:处理善后,可以不要finally,假设IO流,资源,关闭

    4. throw:抛出异常

    5. throws

    public static void main(String[] args) {
        int a =1;
        int b =0;
        //try{}catch(){}一般来说程序遇见异常就会直接停止运行,
        // 但是try-catch可以不停止继续往下运行
        try {
            System.out.println(a/b);
        } catch (Exception e) {
            e.printStackTrace();//打印错误栈的信息
        }
    }
  4. 异常报告:

    1. ArithmeticException---算数异常

  5. CTRL+alt+t:自动生成代码块

自定义异常

 

 

 
posted @ 2022-09-29 10:48  菜鸡前来  阅读(32)  评论(0)    收藏  举报