异常

Exception

三大类型的异常:

  1. 检查性异常

  2. 运行时异常

  3. 错误error

Error:error 类对象由Java虚拟机生成并抛出

Exception:一般由程序逻辑错误引起的

1.异常处理机制

 public static void main(String[] args) {
       int a = 1;
       int b = 0;

       //Ctrl + Alt + T
       try{//监控区域
           if(b==0){
               throw new ArithmeticException();//主动抛出异常
          }
           
           System.out.println(a/b);
      }catch (ArithmeticException e){//想要捕获的异常类型 捕获异常
           System.out.println("错误");
      }catch (Exception e){
           System.out.println("Exception");
      }catch (Throwable t){
           System.out.println("Throwable");
      }
       finally {
           System.out.println("finally");
      }
  }
   
   public void test(int a,int b){
       if(b==0){
           throw new ArithmeticException();//主动抛出异常 一般在方法中使用
           
      }
       System.out.println(a/b);
  }

 

posted @ 2021-07-27 16:58  清钦  阅读(38)  评论(0)    收藏  举报