1自定义异常类方法:
1) 自定义异常类继承现有的异常类
2 )提供一个序列号,提供几个重载的构造器
2 子类重写父类的方法,其抛出的异常类型只能使被重写的方法的异常类的子类或和异常类型一样
3)throw是在手动抛出异常时使用,throws是当声明异常时使用
package lianxi3; public class EcmDef { public static void main(String[] args) { try{ int i = Integer.parseInt(args[0]); int j = Integer.parseInt(args[1]); ecm(i,j); }catch(NumberFormatException e){ System.out.println("输入的数据类型不一致"); } catch(ArrayIndexOutOfBoundsException e){ System.out.println("缺少命令行参数"); } catch(ArithmeticException e){ System.out.println("分母为0"); } catch(EcMinus ec){ System.out.println(ec.getMessage()); } } public static void ecm(int i,int j) throws EcMinus{ if(i<0||j<0){ throw new EcMinus("输入的数不能有负数"); //不是运行异常,必须显式表示 } System.out.println(i/j); } } class EcMinus extends Exception{ static final long serialVersionUID = -33875229948L; public EcMinus() { } public EcMinus(String msg){ super(msg); } }