ArgumentError does not work inside for loops and other blocks

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.

# 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

ArgumentError creates an exception, but does not throw it:

(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
2 Likes

Makes sense. Thanks.

2 Likes