# Capture error from include\_string

**URL:** <https://discourse.julialang.org/t/capture-error-from-include-string/121385>\
**Category:** Internals & Design\
**Tags:** question\
**Created:** [October 16, 2024, 2:02pm UTC](https://discourse.julialang.org/t/capture-error-from-include-string/121385 "2024-10-16T14:02:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 16, 2024, 2:02pm UTC](https://discourse.julialang.org/t/capture-error-from-include-string/121385/1 "2024-10-16T14:02:32Z")

</div>

Consider

```julia
my_e = try
    include_string(Main, "throw(ArgumentError(\"test\"))")
catch e
    e
end

```

The exercise is to extract the `ArgumentError("test")` from `my_e` **using public API**. Is this possible?

Currently it is wrapped in a `LoadError`, the internal structure of which is not public API, and [may change in the future](https://github.com/JuliaLang/julia/blob/c07a40f03c67bbd92db65add4964ee7415453ea4/base/loading.jl#L2782). So `my_e.error` is not what I am looking for.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 16, 2024, 2:37pm UTC](https://discourse.julialang.org/t/capture-error-from-include-string/121385/2 "2024-10-16T14:37:30Z")

</div>

I would just do:

```julia
e isa LoadError ? e.error : e

```

to use `e.error` if it is a `LoadError`. We should really just document this field, since there’s no way to use `LoadError` without it and it has been stable for a decade now. But you’re correct that you shouldn’t assume `include_string` will always wrap exceptions in a `LoadError`.

Alternatively, since `include_string` is nowadays [implemented in pure Julia](https://github.com/JuliaLang/julia/blob/c07a40f03c67bbd92db65add4964ee7415453ea4/base/loading.jl#L2760-L2786) using (it looks like) only public APIs, in principle you could re-implement it (i.e. copy-and-paste the implementation) and get rid of the `LoadError`.

As you noticed, there is a [to-do to get rid of the `LoadError` for `include_string`](https://github.com/JuliaLang/julia/blob/c07a40f03c67bbd92db65add4964ee7415453ea4/base/loading.jl#L2782-L2784) in a future Julia version. Maybe this could be done in 1.x, since it’s not guaranteed by the docs and we got rid of the `LoadError` wrapper for `macroexpand` in 1.7 ([julia#38379](https://github.com/JuliaLang/julia/pull/38379)). Anyone want to file a PR?
