我需要提供单独的异常语句。 1. 空字符串和 2. 有效数字数据
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println(" Enter the Amount of articles to be ordered.");
amount = reader.readLine();
if(amount.trim().isEmpty()){
System.out.println("Amount Entered is Empty");
}
for(int count=0;count<amount.length();count++){
if(!Character.isDigit(amount.charAt(count))){
throw new NumberFormatException();
}
}
order.validateAmount(amount);
}catch(NumberFormatException numbere){
System.out.println("Either Number format is uncorrect or String is Empty, Try Again.");
}
上面的代码为我提供了针对空字符串异常和无效数字数据异常的单个 println() 语句,这是我不想要的。我想要两个异常的单独 println() 语句。怎么得到?
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println(" Enter the Amount of articles to be ordered.");
amount = reader.readLine();
if(amount.trim().isEmpty()){
System.out.println("Amount Entered is Empty");
}
for(int count=0;count<amount.length();count++){
if(!Character.isDigit(amount.charAt(count))){
throw new NumberFormatException();
}
}
order.validateAmount(amount);
}catch(NumberFormatException numbere){
System.out.println("Either Number format is uncorrect or String is Empty, Try Again.");
}
The above code gives me single println() statement for both empty string exception and invalid numeric data exception, which I don't want. I want separate println() statements for both exception. how to get?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用两个不同的异常,例如
NumberFormatException
和IllegalArgumentException
,并执行两个不同的catch-子句
。
<前><代码> ...
if (金额.isEmpty())
抛出新的 IllegalArgumentException();
...
} catch (NumberFormatException numbere) {
System.out.println("任一数字格式不正确,请重试。");
} catch (IllegalArgumentException 空) {
System.out.println("字符串为空,请重试。");
}
使用相同的异常,但具有不同的消息:
<前><代码>尝试{
BufferedReader 阅读器 = 新 BufferedReader(新 InputStreamReader(
系统.in));
System.out.println("请输入要订购的商品数量。");
字符串数量 = reader.readLine();
if (amount.trim().isEmpty()) {
System.out.println("输入的金额为空");
}
if (金额.isEmpty())
throw new IllegalArgumentException("字符串为空。");
for (int count = 0; count < amount.length(); count++)
if (!Character.isDigit(amount.charAt(count)))
throw new IllegalArgumentException("数字格式不正确。");
order.validateAmount(金额);
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage() + " 再试一次。");
}
或者,您可以推出自己的
异常
两个不同的构造函数,以及一个标志,说明异常是否是由于错误的数字或空字符串引起的。You could either use two different exceptions, for instance
NumberFormatException
andIllegalArgumentException
and do two differentcatch
-clauses.Use the same exception but with different messages:
Or, you could roll your own
Exception
with two different constructors, and a flag stating if the exception was due to a bad number or an empty string.由于空字符串是一个“预期”异常,我不会使用异常,而是检查它:
另一个空字符串检查是
amount.trim().length == 0
如果你真的想使用异常:
并添加另一个 catch()
Since an empty string is an 'expected' exception I would not use an exception but check for it:
Another check for emptiness would be
amount.trim().length == 0
If you really want to use exceptions:
and add another catch()