线程“ 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);
}
然后我应该得到:
这不是数字:a
} catch (NumberFormatException e) {
System.err.println(e.getMessage().replaceFirst(".*For input string: ", "This is not a number"));
}
String input="abc"; // declare input such that visible to both try and catch
try {
int a = Integer.parseInt(input);
} catch (NumberFormatException e) {
System.out.println("This is not a number: " + input);
}
String some_string = "12";//retrieval logic
try {
int num = Integer.parseInt(some_string);
} catch (NumberFormatException nfe) {
System.out.println("This is not a number: " + some_string);
}
try {
combinationLength = Integer.parseInt(strCombinationLength);
} catch (NumberFormatException e) {
throw new NumberFormatException("The parameter \"combinationLength \" must be a number");
}
在主体中
try { //some code } catch (NumberFormatException e) { System.out.println(e.getMessage()); }