# Breaking only an inner for loop due to an error

**URL:** https://discourse.julialang.org/t/breaking-only-an-inner-for-loop-due-to-an-error/99698
**Category:** General Usage
**Tags:** question
**Created:** [June 1, 2023, 1:47am UTC](https://discourse.julialang.org/t/breaking-only-an-inner-for-loop-due-to-an-error/99698 "2023-06-01T01:47:43Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Haro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/haro/32/50411_2.png) [@Haro](https://discourse.julialang.org/u/Haro)
#### Post date: [June 1, 2023, 1:47am UTC](https://discourse.julialang.org/t/breaking-only-an-inner-for-loop-due-to-an-error/99698/1 "2023-06-01T01:47:43Z")

</div>

Hi all. I’m trying to run a some code where I am intentionally pushing a differential equations solver that’s called inside a dynamic systems modeling package to fail to converge. I’m trying to explore the boundaries of when this failure happens and the system behavior near the boundary. When the solver fails I get an error that stops my code from advancing, which is fine, except it means I have to change my operating points manually which is time consuming.

I’m testing different combinations of variables which are in nested for loops, and I’d like the inner loop to run until the solver fails to converge in which case the code breaks to the outer loop to test the next set of conditions. Something like:

```julia
set_a = [3.0, 2.0, 1.0]
set_b = [0.25, 0.2, 0.15]
for apples in set_a
        for bananas in set_b
                sys = (system, limit=bananas) # Define the system being studied 
                sim = Simulation( # Establish the length and scenario of the simulation to run
                         ResidualModel, #Type of model used
                         sys, # system
                         (0.0, 40.0), # time span
                         Disturbance(1.0, apples, :P, 0.9);
                         )
                 try
                         execute!(sim, IDA(); abstol = 1e-8) # Command which runs the simulation 
                 catch 
                         break
                  else
                         continue
                  end
                  plot(read_results(sim))
             end
        end

```

However, this seems to break both loops since when I hit a condition I know will cause a convergence error (say 2.0 and 0.2 from above) the normal error message is displayed and the code stops advancing. Does anyone have any ideas on how to resolve this?

---

<div class="post-metadata">

### Author: ![frylock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frylock/32/50213_2.png) [@frylock](https://discourse.julialang.org/u/frylock)
#### Post date: [June 1, 2023, 12:32pm UTC](https://discourse.julialang.org/t/breaking-only-an-inner-for-loop-due-to-an-error/99698/2 "2023-06-01T12:32:14Z")

</div>

There are two _forms_ of the `for` loop construct, see in the [documentation](https://docs.julialang.org/en/v1/manual/control-flow/#man-loops) and also [here](https://en.wikibooks.org/wiki/Introducing_Julia/Controlling_the_flow#Nested_loops).

It seems for you that instead of:

```julia
for apples in set_a
    for bananas in set_b
      # stuff with maybe a break
    end
end

```

You might try:

```julia
for apples in set_a, bananas in set_b
    # stuff with maybe a break
end

```

---

<div class="post-metadata">

### Author: ![Haro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/haro/32/50411_2.png) [@Haro](https://discourse.julialang.org/u/Haro)
#### Post date: [June 25, 2023, 9:47pm UTC](https://discourse.julialang.org/t/breaking-only-an-inner-for-loop-due-to-an-error/99698/3 "2023-06-25T21:47:01Z")

</div>

For my purposes, the

```julia
for apples in set_a, bananas in set_b
# stuff
end

```

formulation would break both my loops, which was the opposite of what I was looking for. I only wanted to break out of set\_b. Turned out the solution was to

```julia
try 
    read_results(sim)
catch err
    if isa(err, MethodError)
           break
   else 
           continue 
   end
end

```

because if the execute! command failed, it wouldn’t throw an error, but the results would be empty and that does throw an error.
