# Try/catch issue

**URL:** https://discourse.julialang.org/t/try-catch-issue/17512
**Category:** General Usage
**Created:** [November 14, 2018, 12:34pm UTC](https://discourse.julialang.org/t/try-catch-issue/17512 "2018-11-14T12:34:57Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Basosa](https://avatars.discourse-cdn.com/v4/letter/b/c2a13f/32.png) [@Basosa](https://discourse.julialang.org/u/Basosa)
#### Post date: [November 14, 2018, 12:34pm UTC](https://discourse.julialang.org/t/try-catch-issue/17512/1 "2018-11-14T12:34:57Z")

</div>

I am new in Julia and I am trying to understand the way that try/catch control flow is working.  
I need to handle possible errors inside a for loop, correct them and finally do what I should do if there was no error.  
However for some reason I don’t manage to do it.  
Thanks in advance for your help.  
Please find below the conceptual approach I am trying to do:

```julia
A=[1,2]'
for i = 1:10
    try
        B=[1,2,3,4] #Creating an error in purpose for the example
    catch B
        if B > 2 # if error
            B=[1,2] # fix error
            A=vcat(A,B')
        else # else
            A=vcat(A,B')
        end
    end
end
A

```

---

<div class="post-metadata">

### Author: ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)
#### Post date: [November 14, 2018, 12:38pm UTC](https://discourse.julialang.org/t/try-catch-issue/17512/2 "2018-11-14T12:38:28Z")

</div>

Before anything, you should start to indent your code. That makes your code much more readable and it’ll be easier to spot bugs.

---

<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: [November 14, 2018, 1:09pm UTC](https://discourse.julialang.org/t/try-catch-issue/17512/3 "2018-11-14T13:09:20Z")

</div>

> [@Basosa](#):
>
> B=[1,2,3,4] #Creating an error in purpose for the example

Why is this an error? (It shouldn’t.)

> [@Basosa](#):
>
> if B \> 2 # if error

Why is this a check for error? I mean if you are in the catch block you have already caught an error. And `B` is the exception here and probably doesn’t compare to `2`. (Not that `[1, 2, 3, 4]` does either…)

I am guessing but it seems that you do not understand what a exception is or what `try`-`catch` is. You seems to think it’s a syntax to help you do branch/check, it’s not. It’s a way to catch exceptions that you’ve thrown. So `B = [1, 2, 3, 4]`, however unexpected it is for the purpose of your code, will not throw an error and won’t do anything in the `catch` block, which includes both branches of the `if`. And the `catch B` has nothing to do with your `B = [1, 2, 3, 4]`.

If you actually have an exception to catch, you should post the real code. If, IMO more like, you are just looking for a way to check for unexpected value and correct for them, just use branches. You can just get rid of the `try`-`catch` completely. You’ll probably also need to fix the `B > 2` since that might give you an actual error. I’m guessing you want `length(B) > 2` here.

And if my guess is wrong, you can ignore all this but you should describe **in detail** what is your expected behavior (what do you mean by “handle possible errors”, “correct them”, what’s the expected output, etc.)
