java try中的变量,java – 如何知道哪个变量是try块中的罪魁祸首?

在某个try块,我有两个字符串变量,当我使用Integer.parseInt(string1)和Integer.parseInt(string2)时可能会导致NumberFormatException。问题是,如果我抓到一个异常,如何知道哪个字符串是麻烦制造者?我需要得到麻烦制造者的变量名。

这里是一些示例代码:

public class test {

public static void main(String[] args) {

try {

String string1 = "fdsa";

String string2 = "fbbbb";

Integer.parseInt(string1);

Integer.parseInt(string2);

} catch (NumberFormatException e) {

e.printStackTrace();

}

}

}

方法e.printStackTrace()不告诉我变量名;它只是告诉我的麻烦制造者的内容。

java.lang.NumberFormatException: For input string: “fdsa” at

java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)

at java.lang.Integer.parseInt(Integer.java:580) at

java.lang.Integer.parseInt(Integer.java:615) at

test.main(test.java:9) at

sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at

sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at

sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498) at

com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

Process finished with exit code 0

我需要知道变量名称的原因是我需要提示用户发生了什么。例如,通过使用告诉用户string1是错误的

System.out.println(troubleMakerName + "is wrong!")

在我的要求中,用户应该输入

fd=(fileName,maxLength,minLength)

然后我将分析输入字符串并创建一些响应。所以我想检查maxLength和minLength是否会抛出NumberFormatException。在这种情况下,如果minLength有错误,那么我需要提示用户minLength是错误的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值