exceptiondemo8.java

来自「JAVA程序设计课程中各章节的程序实例。」· Java 代码 · 共 56 行

JAVA
56
字号
/**
*A simple user-defined exception handling program.pay more attention!
*2004.12.27. xhcprince
*"The return statement may be used in the catch block to return the control
* back to the exception block.If we use return in the catch block,the statement after the catch"
* block will not be displayed!"
*/

class InrangeException extends Exception
{
	InrangeException(int wrongNum)
	{
		super(wrongNum + " is not a valid number");
	}
}

class Validation
{
	public void Inrange(int num) throws Exception
	{
		if(num>0 && num<1111)
		{
			System.out.println("n is in the range");
		}

		else
		{
			throw new InrangeException(num);
		}
	}
}

class exceptionDemo8
{
	public static void main(String args[])
	{
		int num = 0;

		num = Integer.parseInt("0");	//Input from command prompt!
		Validation obj = new Validation();

		try
		{
			obj.Inrange(num);
		}

		catch(Exception e)
		{
			System.out.println(e.toString());
			return;	//the num will not be displayed!
		}

		System.out.print(num);
	}
}

⌨️ 快捷键说明

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