# ArgumentError does not work inside for loops and other blocks

**URL:** <https://discourse.julialang.org/t/argumenterror-does-not-work-inside-for-loops-and-other-blocks/100427>\
**Category:** New to Julia\
**Tags:** error, error-message\
**Created:** [June 15, 2023, 11:36pm UTC](https://discourse.julialang.org/t/argumenterror-does-not-work-inside-for-loops-and-other-blocks/100427 "2023-06-15T23:36:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sadish-d](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sadish-d/32/48058_2.png) [@sadish-d](https://discourse.julialang.org/u/sadish-d)\
**Post date:** [June 15, 2023, 11:36pm UTC](https://discourse.julialang.org/t/argumenterror-does-not-work-inside-for-loops-and-other-blocks/100427/1 "2023-06-15T23:36:57Z")

</div>

In the examples below, A1. and A2. which use `error` break the program and show the error message. B1. and B2., which use `ArgumentError` do not. I’m using Julia 1.9.0. I’ve tried both of these inside functions, in case that makes a difference.

Is this a bug or am I missing something? I’ve switched to using `error` for now.

```julia
# A1.
begin
error("err")
end

# A2.
for i in 1:2
error("err")
end

# B1.
begin
ArgumentError("err")
end

# B2.
for i in 1:2
ArgumentError("err")
end

```

---

<div class="post-metadata">

**Author:** ![mbaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbaz/32/17295_2.png) [@mbaz](https://discourse.julialang.org/u/mbaz)\
**Post date:** [June 15, 2023, 11:57pm UTC](https://discourse.julialang.org/t/argumenterror-does-not-work-inside-for-loops-and-other-blocks/100427/2 "2023-06-15T23:57:11Z")

</div>

`ArgumentError` creates an exception, but does not throw it:

```julia
(v1.9) julia> ex = ArgumentError("Crash!")
ArgumentError("Crash!")

(v1.9) julia> throw(ex)
ERROR: ArgumentError: Crash!
Stacktrace:
 [1] top-level scope
   @ REPL[3]:1
 [2] top-level scope
   @ ~/bin/julia-1.9.0/share/julia/stdlib/v1.9/REPL/src/REPL.jl:1416

```

---

<div class="post-metadata">

**Author:** ![sadish-d](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sadish-d/32/48058_2.png) [@sadish-d](https://discourse.julialang.org/u/sadish-d)\
**Post date:** [June 16, 2023, 12:13am UTC](https://discourse.julialang.org/t/argumenterror-does-not-work-inside-for-loops-and-other-blocks/100427/3 "2023-06-16T00:13:27Z")

</div>

Makes sense. Thanks.
