# Exceptions in macros in Julia-0.7/1.0

**URL:** <https://discourse.julialang.org/t/exceptions-in-macros-in-julia-0-7-1-0/14145>\
**Category:** General Usage\
**Tags:** macros, exception\
**Created:** [August 27, 2018, 4:06pm UTC](https://discourse.julialang.org/t/exceptions-in-macros-in-julia-0-7-1-0/14145 "2018-08-27T16:06:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![traktofon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/traktofon/32/591_2.png) [@traktofon](https://discourse.julialang.org/u/traktofon)\
**Post date:** [August 27, 2018, 4:06pm UTC](https://discourse.julialang.org/t/exceptions-in-macros-in-julia-0-7-1-0/14145/1 "2018-08-27T16:06:50Z")

</div>

Consider a macro that throws an exception:

```julia
macro aaa(); throw(ArgumentError("invalid")); end

```

Calling this macro in Julia 0.6 results in

```julia
julia> @aaa
ERROR: ArgumentError: invalid

```

while in 0.7/1.0 it has changed to (with no mention in NEWS.md)

```julia
julia> @aaa
ERROR: LoadError: ArgumentError: invalid
Stacktrace:
 [1] @aaa(::LineNumberNode, ::Module) at ./REPL[2]:1
in expression starting at REPL[3]:1

```

This poses a problem when testing whether the correct type of exception is thrown by the macro. AFAIK testing for exceptions in macros needs an `eval`, so this works in 0.6:

```julia
julia> using Base.Test
julia> @test_throws ArgumentError eval(:(@aaa))
Test Passed
      Thrown: ArgumentError

```

but in 0.7/1.0 it fails:

```julia
julia> using Test
julia> @test_throws ArgumentError eval(:(@aaa))
Test Failed at REPL[5]:1
  Expression: eval($(Expr(:quote, :(#= REPL[5]:1 =# @aaa))))
    Expected: ArgumentError
      Thrown: LoadError
ERROR: There was an error during testing

```

i.e. I can now only test for `LoadError` but cannot distinguish different types of exceptions any more. Is there any recommended way for testing the exception type thrown by macros in 0.7/1.0?

BTW, according to the help text, a `LoadError` should be thrown if

> An error occurred while includeing, requireing, or using a file […]

I’m not sure whether macro evaluation belongs to that category, maybe the help text could be updated.

---

<div class="post-metadata">

**Author:** ![traktofon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/traktofon/32/591_2.png) [@traktofon](https://discourse.julialang.org/u/traktofon)\
**Post date:** [August 27, 2018, 4:24pm UTC](https://discourse.julialang.org/t/exceptions-in-macros-in-julia-0-7-1-0/14145/2 "2018-08-27T16:24:08Z")

</div>

A quick&dirty solution seems to be:

```julia
macro tmt(typ,expr) # test-macro-throws
   quote
      @test_throws $typ begin
         try
            $expr
         catch e
            rethrow(e.error)
         end
      end
   end
end

```

which works correctly in my test case:

```julia
julia> @tmt ArgumentError eval(:(@aaa))
Test Passed
      Thrown: ArgumentError

```

but I’d welcome any suggestions for improvement of that macro, or alternatives.
