⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gradeexceptiondemo.java

📁 java入门经典教程 java入门经典教程 java入门经典教程
💻 JAVA
字号:
// 例3.3.3 GradeExceptionDemo.java
class MinusException extends ArithmeticException  // 继承了算术异常类
{
	public MinusException()
	{
	}
	public MinusException(String str)
	{
		super(str);
	}
}
class OverException extends ArithmeticException
{
	private int over=0;
	public OverException()
	{
	}
	public OverException(String str)
	{
		super(str);
	}
	public OverException(String str,int i)
	{
		super(str);
		over = i;
	}
	public void getInfo()
	{
		System.out.println(over+ "这个成绩是不合法的");
	}
}
class JudgeScore
{
	public void score(int i) throws MinusException,OverException
	{
		if (i<0)
			throw new MinusException("分数不能为负数");
		if (i>100)
			throw new OverException("分数不能超过100",i);
        System.out.println(i+" 这个分数是合法的");
	}
}
class GradeExceptionDemo
{
	public static void main(String[] args)
	{	
		JudgeScore js = new JudgeScore();
		for(int i=0;i<args.length;i++)
			try
			{
				js.score(Integer.parseInt(args[i]));
			}catch(MinusException me){
				System.err.println(me);
			}catch(OverException oe){
				System.err.println(oe);
				oe.getInfo();
			}catch(ArithmeticException ae){
				System.err.println("Other ArithmeticException");
			}
		System.out.println("The program is over");
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -