7-3 jmu-Java-06异常-04-自定义异常(综合)

定义IllegalScoreException异常类,代表分数相加后超出合理范围的异常。该异常是checked exception,即希望该异常一定要被捕获处理。

定义IllegalNameException异常类,代表名字设置不合理的异常。该异常是unchecked exception

定义Student类。

属性:

private String name;
private int score;

方法:

toString          //自动生成
setter/getter     //自动生成
改造setName       //如果姓名首字母为数字则抛出IllegalNameException
public int addScore(int score)  //如果加分后分数<0 或>100,则抛出IllegalScoreException,加分不成功。

main方法

  1. 输入new则新建学生对象。然后输入一行学生数据,格式为姓名 年龄,接着调用setName,addScore。否则跳出循环。
  2. setName不成功则抛出异常,并打印异常信息,然后继续下一行的处理。
  3. addScore不成功则抛出异常,并打印异常信息,然后继续下一行的处理。如果2、3都成功,则打印学生信息(toString)
  4. 如果在解析学生数据行的时候发生其他异常,则打印异常信息,然后继续下一行的处理。
  5. Scanner也是一种资源,希望程序中不管有没有抛出异常,都要关闭。关闭后,使用System.out.println("scanner closed")打印关闭信息

注意:使用System.out.println(e);打印异常信息,e为所产生的异常。

输入样例:

new
zhang 10
new
wang 101
new
wang30
new
3a 100
new
wang 50
other

输出样例:

Student [name=zhang, score=10]
IllegalScoreException: score out of range, score=101
java.util.NoSuchElementException
IllegalNameException: the first char of name must not be digit, name=3a
Student [name=wang, score=50]
scanner closed

 参考代码如下:

import java.util.Scanner;

class IllegalNameException extends Exception{//自定义异常类,继承Exception类
    public IllegalNameException(){}
    public IllegalNameException(String msg){super(msg);}
    public IllegalNameException(String msg, Throwable cause){super(msg, cause);}
    public IllegalNameException(Throwable cause){super(cause);}
}
class IllegalScoreException extends  Exception{
    public IllegalScoreException(){}
    public IllegalScoreException(String msg){super(msg);}
    public IllegalScoreException(String msg, Throwable cause){super(msg, cause);}
    public IllegalScoreException(Throwable cause){super(cause);}
}
class Stu{
    private String name; private int score; private int flag = 0;
    public int getFlag(){return this.flag;}
    public Stu(String name, int score)throws Exception{this.setName(name); this.addScore(score);}
    public String getName(){return this.name;}
    public int getScore(){return this.score;}
    public void setScore(int score){this.score = score;}
    public void setName(String name) throws IllegalNameException{
        try{
            char c = name.charAt(0);
            if(c>'0'&&c<'9'){
                throw new IllegalNameException();//抛出异常
            }//若抛出异常则不修改标志位,flag为0
            this.name = name; flag = 1;//标志位为1才能进行addScore()
        }catch(IllegalNameException e){//异常处理
            System.out.println("IllegalNameException: the first char of name must not be digit, name="+name);
        }
    }
    public int addScore(int score){
        if(flag==1){
            try {
                this.score += score;
                if(this.score < 0 || this.score > 100){throw new IllegalScoreException();}
            }
            catch(IllegalScoreException e){
                flag = 0;//抛出异常即处理,而标志位为0,结合主程序,不输出学生信息
                System.out.println("IllegalScoreException: score out of range, score="+this.score);
            }
        }
        return this.score;
    }
    public String toString(){
        return "Student [name="+this.name+", score="+this.score+"]";
    }
}
public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in); String str = null;
        while(true){
            str = sc.nextLine();
            if(str.equals("new")){
                String[] s = sc.nextLine().split(" ");
                if(s.length!=2){
                    System.out.println("java.util.NoSuchElementException");
                }
                else{
                    try{
                        Stu stu = new Stu(s[0], Integer.parseInt(s[1]));
                        if(stu.getFlag()==1){//setName()和addScore()都合法才能赋标志位为1
                            System.out.println(stu);
                        }
                    }catch(Exception e){
                        System.out.println(e);
                    }
                }
            }
            else{
                System.out.println("scanner closed");
                sc.close();//关闭输出流
                System.exit(0);
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值