# \`test\_throws\` with macros after PR 23533

**URL:** <https://discourse.julialang.org/t/test-throws-with-macros-after-pr-23533/5878>\
**Category:** General Usage\
**Created:** [September 14, 2017, 2:04am UTC](https://discourse.julialang.org/t/test-throws-with-macros-after-pr-23533/5878 "2017-09-14T02:04:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![fcard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fcard/32/1882_2.png) [@fcard](https://discourse.julialang.org/u/fcard)\
**Post date:** [September 14, 2017, 2:04am UTC](https://discourse.julialang.org/t/test-throws-with-macros-after-pr-23533/5878/1 "2017-09-14T02:04:40Z")

</div>

Due to a recent change ([this one](https://github.com/JuliaLang/julia/pull/23533/files), I think?), errors resulting from a macro seem to be wrapped inside a `LoadError`

```julia
macro m()
  error("err")
end

```

0.6:

```julia
julia> @m
ERROR: err

```

0.7-dev:

````julia
julia> @m
ERROR: LoadError: err
Stacktrace:
 [1] error(::String) at ./error.jl:33
 [2] @m(::LineNumberNode, ::Module) at ./REPL[1]:2
while loading REPL[2], in expression starting on line 1```

````

The stack traces are really useful for debugging, but now I can’t use `@test_throws` to test for a specific error in macros.

```julia
using Base.Test

macro m(x::Expr)
  x.head == :tuple || throw(ArgumentError("@m requires a tuple!"))
  x
end

```

```julia
julia> @test_throws ArgumentError @eval @m [1,2,3]
Test Failed
  Expression: #= REPL[11]:1 =# @eval #= REPL[11]:1 =# @m([1, 2, 3])
    Expected: ArgumentError
      Thrown: LoadError
ERROR: There was an error during testing

```

Workarounds exist, of course:

```julia
@test_throws ArgumentError try @eval @m [1,2,3] catch err; throw(err.error) end

```

But that to me seems to be reintroducing the very thing that `@test_throws` was supposed to abstract away, maybe.

I have nothing else to say, just thought this was something worth bringing up. (apologies if I am mistaken)

* * *

I spent an hour thinking about it but still have no clue what this post classifies as or where I should put it, (it’s not a proposal or a question and the relevance it has to Julia is TBD) feel free to move this to where it’s appropriate or/and throw it in the garbage.

---

<div class="post-metadata">

**Author:** ![davidavdav](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidavdav/32/1065_2.png) [@davidavdav](https://discourse.julialang.org/u/davidavdav)\
**Post date:** [April 23, 2019, 4:59pm UTC](https://discourse.julialang.org/t/test-throws-with-macros-after-pr-23533/5878/2 "2019-04-23T16:59:42Z")

</div>

I just ran into the same problem developing a macro package, in julia 1.1. Are there any more recent insights than the workaround mentioned above (for which thanks, BTW).

---

<div class="post-metadata">

**Author:** ![bsxfan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bsxfan/32/6914_2.png) [@bsxfan](https://discourse.julialang.org/u/bsxfan)\
**Post date:** [April 24, 2019, 1:13pm UTC](https://discourse.julialang.org/t/test-throws-with-macros-after-pr-23533/5878/3 "2019-04-24T13:13:45Z")

</div>

The workaround suggested by @fcard does not always work. It depends where the error is thrown. If the exception if thrown by the macro itself, then that exception is wrapped inside a `LoadError `, but if the code that is generated by the macro throws the exception, the wrapping does not occur and the workaround fails.

At present @davidavdav and I are using another workaround. Instead of throwing the exception in the to-be-tested macro, we `return :(throw(some_exception))`. Then unmodified `@test_throws` works as intended, without having to dig `some_exception` out of the enclosing `LoadError`.
