《第一行代码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)

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值