1. 自定义异常
public class MyException extends Exception{
public MyException() {
}
public MyException(String message) {
super(message);
}
}
public class StudentDemo {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入学生的成绩:");
int score=sc.nextInt();
Teacher t=new Teacher();
try {
t.check(score);
} catch (MyException e) {
e.printStackTrace();
}
}
}
public class Teacher {
public void check(int score) throws MyException{
if (score>100||score<0) {
throw new MyException("分数必须在0到100之间");
}else {
System.out.println("分数没有问题");
}
}
}