myexceptest.java

来自「关于java异常处理方面的小例子」· Java 代码 · 共 50 行

JAVA
50
字号
package myException;

import java.io.*;

class OutBoundsException extends Exception
{
	OutBoundsException(String mes)
	{
		super(mes);
	}
}

class Check
{
	String ChecktheNum(int n)throws OutBoundsException
	{
		Integer N=new Integer(n);
		if(n>30||n<20)
			throw new OutBoundsException("the number is out of bound");
		else
			return "the number"+N.toString()+"is in the bound";

	}
}

public class MyExcepTest 
{

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		// TODO 自动生成方法存根
       try
       {
    	   Check c=new Check();
    	   System.out.println("以下是合法数据");
    	   System.out.println(c.ChecktheNum(25));
    	   System.out.println("以下是非法数据");
    	   System.out.println(c.ChecktheNum(5));
       }
       catch(OutBoundsException e)
       {
    	   System.out.println(e.toString());
       }
	}

}

⌨️ 快捷键说明

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