Java自定义异常

Java自定义异常,第1张

Java自定义异常
public class Homework3 {

	public static void main(String[] args) {

		Compute compute = new Compute();

		try {
			compute.sum(90);
			compute.sum(78);
			compute.sum(101);
			compute.sum(30);
		} catch (MyException e) {
			System.out.println(e.getMessage());
		}

	}

}

class Compute {
	int totalScore = 0;

	public void sum(int x) throws MyException {

		if (x >= 0 && x <= 100) {
			System.out.println("输入成绩为:" + x);
			this.totalScore += x;
			System.out.println("当前总成绩为:" + this.totalScore);
		} else {
			throw new MyException("输入有误,成绩不在0到100之间");
		}

	}

}

class MyException extends Exception {

	MyException(String msg) {
		super(msg);
	}

}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4744628.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-08
下一篇 2022-11-08

发表评论

登录后才能评论

评论列表(0条)

保存