作业-异常处理2
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了作业-异常处理2相关的知识,希望对你有一定的参考价值。
建立exception包,建立Bank类,类中有变量double balance表示存款,Bank类的构造方法能增加存款,Bank类中有取款的发方法withDrawal(double dAmount),当取款的数额大于存款时,抛出InsufficientFundsException,取款数额为负数,抛出NagativeFundsException,如new Bank(100),表示存入银行100元,当用方法withdrawal(150),withdrawal(-15)时会抛出自定义异常。
package Bank; public class Bank { private static double balance; Bank(){ }; Bank(double balance){ this.balance=balance; } public static void withDrawal(double dAmount) throws InsufficientFundsException,NagativeFundsException{ if(dAmount>balance){ throw new InsufficientFundsException(); } if(dAmount<0){ throw new NagativeFundsException(); } } public static void main(String[] args){ Bank b=new Bank(100); System.out.println("我有"+balance+"元存款!"); try{ withDrawal(150); }catch(InsufficientFundsException | NagativeFundsException e){ e.printStackTrace(); } try{ withDrawal(-15); }catch(NagativeFundsException |InsufficientFundsException e){ e.printStackTrace(); } } } package Bank; public class InsufficientFundsException extends Exception { public String getMessage(){ return "您的余额不足!"; } } package Bank; public class NagativeFundsException extends Exception{ public String getMessage(){ return "取款金额不能为负数!"; } }
以上是关于作业-异常处理2的主要内容,如果未能解决你的问题,请参考以下文章
PCL异常处理:pcl 1.8.13rdpartyoostincludeoost-1_64oost ypeofmsvc ypeof_impl.hpp(125): error(代码片段