Java中的异常处理(三) - 自定义异常处理

时间:2023-03-09 22:03:25
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();
}
}