gpt4 book ai didi

java - 当 NumberFormatException 时如何使用有问题的输入字符串打印自定义消息

转载 作者:行者123 更新时间:2023-12-02 06:19:12 26 4
gpt4 key购买 nike

线程“main”中出现异常java.lang.NumberFormatException:对于输入字符串:“a”

如何访问导致 NumberFormatException 以这种形式打印自定义错误消息的输入字符串:

try {
/* some code which includes many Integer.parseInt(some_string); */
}
catch (NumberFormatException nfe) {
System.out.println("This is not a number: " + input_string_which_causing_the_error);
}

那么我应该得到:

This is not a number: a

最佳答案

您可以从异常中提取它:

} catch (NumberFormatException e) {
System.err.println(e.getMessage().replaceFirst(".*For input string: ", "This is not a number"));
}

关于java - 当 NumberFormatException 时如何使用有问题的输入字符串打印自定义消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21162434/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com