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


in try block, have 2 string variables cause numberformatexception when user integer.parseint(string1) andinteger.parseint(string2). question is, if catch exception, how know string troublemaker? need troublemaker's variable name.

here 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 method e.printstacktrace() doesn't tell me variable name; tells me content of troublemaker.

java.lang.numberformatexception: input string: "fdsa" @ java.lang.numberformatexception.forinputstring(numberformatexception.java:65) @ java.lang.integer.parseint(integer.java:580) @ java.lang.integer.parseint(integer.java:615) @ test.main(test.java:9) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:498) @ com.intellij.rt.execution.application.appmain.main(appmain.java:147)

process finished exit code 0

the reason need know variable's name need prompt user what's going on. instance, tell user string1 wrong using

system.out.println(troublemakername + "is wrong!") 

in requirements, user should input

fd=(filename,maxlength,minlength) 

then analyse input string , create responses. i'd check whether maxlength , minlength throw numberformatexception. in case, if minlength has wrong, need prompt user minlength wrong.

you having xy-problem.

you don't want read actual variable name. want able validate input , give reasonable error messages 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 maximum length, input not number"); }  try {     minlength = integer.parseint(minlengthinput); } catch (numberformatexception nfe) {     errors.add("invalid input minimum length, input not number"); }  // show error strings user 

not throwing exceptions directly collecting them allows notify user invalid inputs @ once (maybe highlight related fields red color) instead of having them fix 1 input, trying submit again, , see input wrong.

instead of strings user own data struture containing information of related field etc., gets out of scope. main gist is: use 2 try-catch blocks, , able differentiate field errorneous.

if more inputs involved, can refactor loop.


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -