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

📄 exception3.java

📁 课本异常处理实例。各种异常处理
💻 JAVA
字号:
//捕获多个异常
public class Exception3
{
	public static int Sum(int n)
	{
		if (n < 0)
			throw new IllegalArgumentException("n应该为正整数!");
		int s = 0;
		for (int i=0; i<=n; i++) s = s + i;
			return s;
	}
	public static void main(String args[])
	{
		try
		{
			int n = Integer.parseInt(args[0]);//注意要在DOS命令行中带参数运行
			System.out.println(Sum(n));//注意调用了含有异常的方法Sum()
		}
		catch (ArrayIndexOutOfBoundsException e)
		{
			System.out.println("命令行为:"+"java Exception3 <number>");
		}
		catch (NumberFormatException e2)
		{
			System.out.println("参数<number>应为整数!");
		}
		catch (IllegalArgumentException e3)
		{
			System.out.println("错误参数:"+e3.toString());//返回异常的信息
		}
		finally
		{
			System.out.println("程序结束!");
		}
	}
}

⌨️ 快捷键说明

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