public class ScoreException extends Exception {
//继承Exception或者RuntimeException
public ScoreException(){
//使用super调用无参构造器
super();
}
public ScoreException(String message){
//使用super调用有参构造器
super(message);
}
}
import java.util.Scanner;
public class ThrowScoreException {
public static void main(String[] args) {
try {
Scanner input = new Scanner(System.in);
int score = 0;
System.out.println("请输入成绩");
if (input.hasNextInt()) {
if (score < 0 || score > 100) {
throw new ScoreException("请输入正确的成绩信息");
} else {
score = input.nextInt();
System.out.println("您输入的成绩是:" + score);
}
} else {
System.out.println("请输入正确的整数成绩信息");
}
} catch (ScoreException inNormal) {
System.out.println(inNormal.getMessage());
}
}
}