Groovy web console

subscribe to the feed Subscribe
to this
site

variable scope & redefinitions

Published 2 months ago by Andrew J. Richardson
Actions Execute script  ▶ Edit in console Back to console Show/hide line numbers View recent scripts
def allowed() {
    try {
        def var = 'howdy'
        throw Exception
    }
    catch (Exception) {
        println('oops')
    }
    def var = 'hello'
    println(var)
}

def not_allowed() {
    def var = 'howdy'
    try {
        //uncomment below to see compilation error: The current scope already contains a variable of the name var
        //def var = "bye"
        throw Exception
    }
    catch (Exception) {
        println(var)
    }
}

allowed()
not_allowed()