# Scoping rules in try blocks

**URL:** https://discourse.julialang.org/t/scoping-rules-in-try-blocks/5143
**Category:** General Usage
**Tags:** question
**Created:** [July 30, 2017, 11:59pm UTC](https://discourse.julialang.org/t/scoping-rules-in-try-blocks/5143 "2017-07-30T23:59:22Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![musm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/musm/32/3675_2.png) [@musm](https://discourse.julialang.org/u/musm)
#### Post date: [July 30, 2017, 11:59pm UTC](https://discourse.julialang.org/t/scoping-rules-in-try-blocks/5143/1 "2017-07-30T23:59:22Z")

</div>

Could anyone explain the scoping rules below (and if it is expected)? They seem rather unintuitive to me

```julia
julia> let
       try
           x = 2
       end
       y = x + 1
       end
ERROR: UndefVarError: x not defined

julia> let
       try
           x = 2
       end
       x = x + 1
       end
3

```

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [July 31, 2017, 12:08am UTC](https://discourse.julialang.org/t/scoping-rules-in-try-blocks/5143/2 "2017-07-31T00:08:47Z")

</div>

Not just `try` block.  
Assignment without `global` makes the variable local in the scope so in the first case `x` is local to the `try` and in the second case the `x` is local to the `let`.  
This is similar to [Race condition caused by variable scope getting lifted from a multithreaded context · Issue #14948 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/14948)

---

<div class="post-metadata">

### Author: ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)
#### Post date: [July 31, 2017, 7:25am UTC](https://discourse.julialang.org/t/scoping-rules-in-try-blocks/5143/3 "2017-07-31T07:25:14Z")

</div>

But isn’t this still unintuitive that you can change the scope of `x` by calling `x=x`? Seems like a hack with potentially little harm however, because one would not call `x = f(x)` when `x` is not defined in the current scope unless referring to the previous `x` as the input to `f` so it kind of works out anyways, but still weird.

---

<div class="post-metadata">

### Author: ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)
#### Post date: [August 3, 2017, 10:16pm UTC](https://discourse.julialang.org/t/scoping-rules-in-try-blocks/5143/4 "2017-08-03T22:16:29Z")

</div>

The scope is defined by the outer most block that has an assignment

---

<div class="post-metadata">

### Author: ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)
#### Post date: [August 4, 2017, 3:22am UTC](https://discourse.julialang.org/t/scoping-rules-in-try-blocks/5143/5 "2017-08-04T03:22:50Z")

</div>

Interesting.
