** 异常处理五个关键字:try catch finally throw throws **
package com.le.exception; public class Test { public static void main(String[] args) { try{ new Test().test(1,0); }catch(ArithmeticException e){ e.printStackTrace(); } } public void test(int a, int b) throws ArithmeticException{ if(b==0){ throw new ArithmeticException(); } } /* int a = 1; int b = 0; //ctrl+alt+t try{//try监控区 if(b==0){ throw new ArithmeticException();//主动的抛出异常 } //System.out.println(a/b); }catch(ArithmeticException e){//catch (想要捕获的异常类型)捕获异常 System.out.println("程序不能出现异常,b变量不能为0"); }catch(Throwable e){ } finally{//处理善后工作 System.out.println("finally"); } //finally 可以不要,可以用来关闭资源 */ }