自定义异常类,封装取款时发生异常的情况。定义银行账号类,当取款发生异常时抛出该异常,并写测试类测试。lassDepositExceptionextendsException{publicDepositException(){super();//TODOAuto-generatedconstructorstub}publicDepositException(Stringmessage){super(message);//TODOAuto-generatedconstructorstub}}.;/***银行账户的父类,定义共同的属性和方法*是抽象类,定义子类的共同特征****@authorwxh**/ount{//ountId;privateStringpwd;privatedoublebalance;//日志数组privateString[]logInfo=newString[3];privateintlogIndex;//ount(){//使用this调用本类重载的其他构造方法this("0000","123456",);("ount()");}ount(ountId,Stringpwd,doublebalance){super();=accountId;=pwd;=balance;("ount(ountId,Stringpwd,doublebalance)");}//ountId(){ountId;}publicStringgetPwd(){returnpwd;}publicvoidsetPwd(Stringpwd){=pwd;}publicdoublegetBalance(){returnbalance;}publicvoidsetBalance(doublebalance){=balance;}//存款,不使用异常//publicbooleandeposit(doubleamount){//booleanflag=true;//if(amount>0&&amount<=10000){//(this,"存款成功,金额:"+amount);//balance+=amount;//}else{//flag=false;//}//returnflag;//}//存款,使用异常//publicvoiddeposit(doubleamount){////if(amount>0&&amount<=10000){//(this,"存款成功,金额:"+amount);//balance+=amount;//}else{////抛出异常//try{//thrownewException("存款失败");//}catch(Exceptione){//("存款金额不能为负数,重新输入。");//}//}////}////存款,throws//publicvoiddeposit(doubleamount)throwsException{////if(amount>0&&amount<=10
Java异常处理 来自淘豆网m.daumloan.com转载请标明出处.