# Is it necessary to use "global" with try/catch inside a function?

**URL:** https://discourse.julialang.org/t/is-it-necessary-to-use-global-with-try-catch-inside-a-function/50300
**Category:** General Usage
**Tags:** scope
**Created:** [November 17, 2020, 1:34pm UTC](https://discourse.julialang.org/t/is-it-necessary-to-use-global-with-try-catch-inside-a-function/50300 "2020-11-17T13:34:11Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Jacob\_Stevens](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacob_stevens/32/19496_2.png) [@Jacob\_Stevens](https://discourse.julialang.org/u/Jacob_Stevens)
#### Post date: [November 17, 2020, 1:34pm UTC](https://discourse.julialang.org/t/is-it-necessary-to-use-global-with-try-catch-inside-a-function/50300/1 "2020-11-17T13:34:11Z")

</div>

I’m running a simple find\_zero(), and to help me identify errors the core of the function being solved is inside a try/catch circuit:

```julia
function getA(input)

       # then main simulation
       println("Running 1982 onwards ")
       try
              results = S82to20(s82, input, state1981, k81);
       catch err
              print(err)
              println("with input ", input)
              throw(error())
       end
       
       cap = results[4];

       return f(cap)
end

solution = find_zero(getA, iniInput)

```

However, I get the error “results not found”. I understand this is a scoping problem, and the easiest fix is to prefix “global” inside the try/catch. But I only want “results” to fall within function scope, not universal scope. So my questions are:

- If I prefix “global”, does that place results in function scope or top-level scope?
- If the former, how would I (hypothetically) place it in top-level scope instead?
- If the latter, is there an alternative to “global” that places it in function scope?

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [November 17, 2020, 1:40pm UTC](https://discourse.julialang.org/t/is-it-necessary-to-use-global-with-try-catch-inside-a-function/50300/2 "2020-11-17T13:40:31Z")

</div>

Initializing results is enough, no need for global:

```julia
function getA(input)
  results=0 #or whatever suits
  try ...

```

---

<div class="post-metadata">

### Author: ![yha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yha/32/3502_2.png) [@yha](https://discourse.julialang.org/u/yha)
#### Post date: [November 17, 2020, 1:44pm UTC](https://discourse.julialang.org/t/is-it-necessary-to-use-global-with-try-catch-inside-a-function/50300/4 "2020-11-17T13:44:36Z")

</div>

Or just `local results`:

```julia
function getA(input)
  local results
  try
    ...

```

or

```julia
function getA(input)
 results = try
    ...
```
