《第一行代码java》课后编程题(第六章)

1.编写应用程序,从命令行输入两个小数参数,求它们的商。要求程序中捕获NumberForamt Exception异常和Arithmetic Exception异常。
NumberForamt Exception异常:是指数字转换异常。
Arithmetic Exception异常:是指算术异常。
代码如下


public class Test {
	public static void main(String[] args) throws Exception{
		String numString = "a";
		System.out.println(Integer.parseInt(numString));
	}
}

所报错误

Exception in thread "main" java.lang.NumberFormatException: For input string: "a"
	at java.lang.NumberFormatException.forInputString(Unknown Source)
	at java.lang.Integer.parseInt(Unknown Source)
	at java.lang.Integer.parseInt(Unknown Source)
	at Test.main(Test.java:5)

代码如下

public class Test {
	public static void main(String[] args) {
		int x = 0;
		int y = 0;
		System.out.println(x / y);
	}
}

错误如下

Exception in thread "main" java.lang.ArithmeticException: / by zero
	at Test.main(Test.java:5)

你可能感兴趣的:(java)