Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
170 views
in Technique[技术] by (71.8m points)

java - How to know which variable is the culprit in try block?

In a certain try block, I have two String variables which could cause NumberFormatException when I user Integer.parseInt(string1) andInteger.parseInt(string2). The question is, if I catch an exception, how to know which string is the troublemaker? I need to get the troublemaker's variable name.

Here is some example code:

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();
        }
        }
    }

And the method e.printStackTrace() doesn't tell me the variable name; it just tells me the content of the troublemaker.

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

The reason that I need to know the variable's name is that I need to prompt the user what's going on. For instance, tell the user that string1 is wrong by using

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

In my Requirements, the user should input

fd=(fileName,maxLength,minLength)

then I will analyse the input string and create some responses. So I'd like to check whether the maxLength and minLength will throw NumberFormatException. In this case, if minLength has something wrong, then I need to prompt the user that the minLength is wrong.

question from:https://stackoverflow.com/questions/38630111/how-to-know-which-variable-is-the-culprit-in-try-block

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are having an XY-Problem.

You don't want to read the actual variable name. You want to be able to validate input and give reasonable error messages to your user.

String fileName, maxLengthInput, minLengthInput;
int maxLength, minLength;

List<String> errors = new ArrayList<>();

try {
    maxLength = Integer.parseInt(maxlengthInput);
} catch (NumberFormatException nfe) {
    errors.add("Invalid input for maximum length, input is not a number");
}

try {
    minLength = Integer.parseInt(minlengthInput);
} catch (NumberFormatException nfe) {
    errors.add("Invalid input for minimum length, input is not a number");
}

// show all error strings to the user

Not throwing the exceptions directly but collecting them allows you to notify the user about all invalid inputs at once (maybe highlight the related fields with red color) instead of having them fix one input, trying to submit again, and then to see that another input is also wrong.

Instead of Strings you could user your own data struture containing information of the related field etc., but that quickly gets out of scope. The main gist is: use two try-catch blocks, and you are able to differentiate which field is errorneous.

If more inputs are involved, you can refactor this into a loop.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...