标签:code 实例 xtend static on() 范围 tac ati 构造
/* 自定义异常:ScoreException */ public class ScoreException extends Exception { //无参构造 public ScoreException(){} //带参构造 public ScoreException(String message){ super(message); } }
/* 定义教师类 */ public class Teacher { public void checkScore(int score) throws Exception{ if (score < 0 || score > 100){ throw new ScoreException("你输入的分数有误"); }else { System.out.println("分数正常"); } } }
/* 测试类 */ import java.util.Scanner; public class TeacherTest { 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.checkScore(score); } catch (Exception e) { e.printStackTrace(); } } }
当输入的成绩不在正常的范围内时,会输出异常提醒
请输入分数:
120
com.itheima_02.ScoreException: 你输入的分数有误
at com.itheima_02.Teacher.checkScore(Teacher.java:6)
at com.itheima_02.TeacherTest.main(TeacherTest.java:13)
标签:code 实例 xtend static on() 范围 tac ati 构造
原文地址:https://www.cnblogs.com/pxy-1999/p/12641969.html