Java中的异常处理(三)

1.异常处理类

package second;

public class MyException extends Exception {

    MyException (){
        
    }
    MyException (String ErrorMsg){
        super(ErrorMsg);
    }
    public void check(){
        System.out.println("都报错了,还处理个毛线!");
    }
}

2.程序

package second;

public class ScoreCount {
    public static void main(String[] args){
        String end = "";
        try{
            end = score(1000);
        }catch(MyException e){
            e.check();
        }
        System.out.println(end);
    }
    /* 可能会爆出异常的方法 */
    static String score(int score) throws MyException {
        if(score <= 50){
            return "不及格!";
        }else if(score <= 80){
            return "优良!";
        }else if(score <= 100){
            return "好评!";
        }
        throw new MyException();
    }
}
原文地址:https://www.cnblogs.com/shibazi/p/3844288.html