While loop with try/catch error in Julia logic problem

I’m trying to write a while loop that tests whether an input is valid or not and it keeps asking if the user insists on the wrong input. If it is a valid one, the program just goes on. This is where i’m at.

        while true
            try
                selected_row = readline()
                selected_row = parse(Int8, selected_row)
                break
            catch e
                if isa(e, LoadError)
                    @warn "type a valid number between [1,8]"
                elseif isa(e, ArgumentError)
                    @warn "type a valid number between [1,8]"
                end
            end
        end

        println("you chose (C|R) ($selected_row)")

The problem is that when the input is correct I get an error.

LoadError: UndefVarError: selected_row not defined

Loops have their own scope - define selected_row outside of the loop via local selected_row to have access to variables otherwise defined inside of the loop if the loop is inside of a function - if it’s in global scope, you will have to use global selected_row. It’s usually better to place all your code in functions though.

See Scope of Variables · The Julia Language for more information about scoping behavior in julia.