# While loop with try/catch error in Julia logic problem

**URL:** <https://discourse.julialang.org/t/while-loop-with-try-catch-error-in-julia-logic-problem/67629>\
**Category:** New to Julia\
**Created:** [September 3, 2021, 6:22am UTC](https://discourse.julialang.org/t/while-loop-with-try-catch-error-in-julia-logic-problem/67629 "2021-09-03T06:22:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![hdcpereira](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hdcpereira/32/28705_2.png) [@hdcpereira](https://discourse.julialang.org/u/hdcpereira)\
**Post date:** [September 3, 2021, 6:22am UTC](https://discourse.julialang.org/t/while-loop-with-try-catch-error-in-julia-logic-problem/67629/1 "2021-09-03T06:22:55Z")

</div>

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.

```julia
        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.

```julia
LoadError: UndefVarError: selected_row not defined

```

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [September 3, 2021, 6:32am UTC](https://discourse.julialang.org/t/while-loop-with-try-catch-error-in-julia-logic-problem/67629/2 "2021-09-03T06:32:03Z")

</div>

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](https://docs.julialang.org/en/v1/manual/variables-and-scoping) for more information about scoping behavior in julia.
