java异常处理——题

1、建立exception包,编写TestException.java程序,主方法中有以下代码,确定其中可能出现的异常,进行捕获处理。

public class YiChang {
    public static void main(String[] args){
        for(int i=0;i<4;i++){
            int  k;
            switch(i){
                case 0: int zero=0;    
                try{
                    k=911/zero;
                }catch(ArithmeticException e){
                    System.out.println("出现算数异常!");
                }
                break;
                case 1: 
                    try{
                        int  b[]=null;
                        k = b[0];
                    }catch(NullPointerException e){
                        System.out.println("出现空指针异常!");
                    }
                    break;
                case 2:
                    int c[]=new int[2];
                    try{
                        k=c[9];
                    }catch(ArrayIndexOutOfBoundsException e){
                        System.out.println("出现数组序号溢出!");
                    }
                    break;
                case 3:
                    try{
                        char  ch="abc".charAt(99);
                    }catch(StringIndexOutOfBoundsException e){
                        System.out.println("出现数据类型转换异常!");
                    }
                    break;
            }
        }
    }
}

2、建立exception包,建立Bank类,类中有变量double  balance表示存款,Bank类的构造方法能增加存款,Bank类中有取款的发方法withDrawal(double dAmount),当取款的数额大于存款时,抛出InsufficientFundsException,取款数额为负数,抛出NagativeFundsException,如new Bank(100),表示存入银行100元,当用方法withdrawal(150),withdrawal(-15)时会抛出自定义异常。

public class InsufficientFundsException extends Exception {
    public String getMessage(){
        return "您的余额不足!";
    }
}
public class NagativeFundsException extends Exception{
    public String getMessage(){
        return "取款金额不能为负数!";
    }
}
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();
        }
    }    
}

 

转载于:https://www.cnblogs.com/jingzhenhua/p/5902377.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值