JAVA小练习77——异常捕获机制--除数为零的算术异常和空指针异常等

    xiaoxiao2023-11-01  29

    class Demo77 { public static void main(String[] args) { div(4,0,null ); } public static void div(int a, int b,int[] arr){ int c = 0; try{ //c = a/b; System.out.println("数组的长度:"+ arr.length); // c = a/b; // jvm发现除数为0的时候,就会创建一个异常的对象。 }catch(ArithmeticException e){ //捕获的异常类型 System.out.println("出了算术异常..."); e.printStackTrace(); //}catch(NullPointerException e){ //System.out.println("出现了空指针异常...."); }catch(Exception e){ // new NullPointerException(); Exception之所以可以捕获任意类型的异常,是因为Exception是所有异常类的父类。 System.out.println("我是急诊室,能治百病..."); } System.out.println("结果:"+ c); }
    最新回复(0)