异常机制处理

发布时间 2023-08-23 17:04:01作者: Y~~~

异常机制处理

1.抛出异常
2.捕获异常

Ctrl+Alt+T

public class Test2 {
   public static void main(String[] args) {


       try {
           new Test2().test(1,0);
      } catch (ArithmeticException e) {
           throw new RuntimeException(e);
      } finally {
           System.out.println("最后");
      }

  }

   // 假设这方法中,处理不了这个异常。方法上抛出异常。
   public void test(int a,int b) throws ArithmeticException{
       if(b==0){
           throw new ArithmeticException(); //主动抛出异常,一般在方法中使用
      }
  }

}