Groovy: Undeclared variable - runtime exception - groovy.lang.MissingPropertyException
Unlike the "strongly typed" languges, Groovy does not check the existance of all the variables at compile time. This can lead to run-time exceptions such as groovy.lang.MissingPropertyException
examples/groovy/undeclared_variable.groovy
print "Input: " def val = System.in.newReader().readLine() as Integer if (val > 10) { println "bigger than 10" } else if (value < 10) { println "smaller than 10" } else { println "it is 10" }
In this script we declare and use a variable called val, but in one point, in the second if-statement, we used the variable name value. Presumably by mistake.
If the input is 10 or more the code runs smoothly, but if the input is less than 10 then we get a run-time exception:
Caught: groovy.lang.MissingPropertyException: No such property: value for class: undeclared_variable groovy.lang.MissingPropertyException: No such property: value for class: undeclared_variable at undeclared_variable.run(undeclared_variable.groovy:6)
Published on 2018-05-27