# Question about macro and how to resolve/esc variables/expressions

**URL:** <https://discourse.julialang.org/t/question-about-macro-and-how-to-resolve-esc-variables-expressions/91309>\
**Category:** General Usage\
**Tags:** macros\
**Created:** [December 6, 2022, 3:21pm UTC](https://discourse.julialang.org/t/question-about-macro-and-how-to-resolve-esc-variables-expressions/91309 "2022-12-06T15:21:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)\
**Post date:** [December 6, 2022, 3:21pm UTC](https://discourse.julialang.org/t/question-about-macro-and-how-to-resolve-esc-variables-expressions/91309/1 "2022-12-06T15:21:20Z")

</div>

Hi all 🙂

Here is a little example code. I’m interested in the local var `_error_msg `:  
`local _error_msg = $(esc(sprint(showerror, error, catch_backtrace())))`  
So what I want is the error and catch\_backtrace as string.

Using `@macroexpand` I see what I want as value. But just using it gives me an `:e`.

(with julia 1.8.3)

```julia
struct MissingDataException <: Exception 
    msg::String
end

Base.showerror(io::IO, ex::MissingDataException) =
    print(
        io, 
        """MissingDataException:
         $(ex.msg)
        """
    )

macro catch_missing_error(error, msg)
    quote
        local _error = $(esc(error))
        local _msg = $(esc(msg))
        local _error_msg = $(esc(sprint(showerror, error, catch_backtrace())))
        if _error isa MissingDataException
            @show _error
            @show _error_msg
        else
            @show _error
            @show _error_msg
        end

    end
end

try
    throw(MissingDataException("err"))
catch e
    @catch_missing_error e "Hallo"
end

try
    throw(MissingDataException("err"))
catch e
    @macroexpand @catch_missing_error e "Hallo"
end

```

Thanks

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [December 6, 2022, 8:19pm UTC](https://discourse.julialang.org/t/question-about-macro-and-how-to-resolve-esc-variables-expressions/91309/3 "2022-12-06T20:19:08Z")

</div>

> [@Impressium](#):
>
> I’m interested in the local var `_error_msg `:  
> `local _error_msg = $(esc(sprint(showerror, error, catch_backtrace())))`

I think what you want is more along the lines of:

```julia
_error_msg = sprint(showerror, $(esc(error)), catch_backtrace())

```

In other words, you want `catch_backtrace` (and `sprint`) to be called at runtime, not at macro expansion time.

Full example:

```julia
julia> macro catch_missing_error(error, msg)
           quote
               local _error = $(esc(error))
               local _msg = $(esc(msg))
               local _error_msg = sprint(showerror, $(esc(error)), catch_backtrace())
               if _error isa MissingDataException
                   @show _error
                   @show _error_msg
               else
                   @show _error
                   @show _error_msg
               end
       
           end
       end
@catch_missing_error (macro with 1 method)

julia> @macroexpand @catch_missing_error e "Hallo"
quote
    #= REPL[1]:3 =#
    local var"#1#_error" = e
    #= REPL[1]:4 =#
    local var"#2#_msg" = "Hallo"
    #= REPL[1]:5 =#
    local var"#3#_error_msg" = Main.sprint(Main.showerror, e, Main.catch_backtrace())
    #= REPL[1]:6 =#
    if var"#1#_error" isa Main.MissingDataException
        #= REPL[1]:7 =#
        begin
            Base.println("_error = ", Base.repr(begin
                        #= show.jl:1047 =#
                        local var"#4#value" = var"#1#_error"
                    end))
            var"#4#value"
        end
        #= REPL[1]:8 =#
        begin
            Base.println("_error_msg = ", Base.repr(begin
                        #= show.jl:1047 =#
                        local var"#5#value" = var"#3#_error_msg"
                    end))
            var"#5#value"
        end
    else
        #= REPL[1]:10 =#
        begin
            Base.println("_error = ", Base.repr(begin
                        #= show.jl:1047 =#
                        local var"#6#value" = var"#1#_error"
                    end))
            var"#6#value"
        end
        #= REPL[1]:11 =#
        begin
            Base.println("_error_msg = ", Base.repr(begin
                        #= show.jl:1047 =#
                        local var"#7#value" = var"#3#_error_msg"
                    end))
            var"#7#value"
        end
    end
end

```
