# What is the best future-proof way to check which function raised a MethodError?

**URL:** <https://discourse.julialang.org/t/what-is-the-best-future-proof-way-to-check-which-function-raised-a-methoderror/48354>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 14, 2020, 10:37am UTC](https://discourse.julialang.org/t/what-is-the-best-future-proof-way-to-check-which-function-raised-a-methoderror/48354 "2020-10-14T10:37:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [October 14, 2020, 10:37am UTC](https://discourse.julialang.org/t/what-is-the-best-future-proof-way-to-check-which-function-raised-a-methoderror/48354/1 "2020-10-14T10:37:43Z")

</div>

I want to ensure that the `MethodError` is raised by by a function in my package, and not downstream.

For example here’s one way:

```julia
julia> e = MethodError(sin, "a")
MethodError(sin, "a", 0xffffffffffffffff)

julia> e.f == sin
true

```

but I’m not sure if this is too internal to be used in a package. Another way is to capture the error string and process it to extract the function, eg something along these lines:

```julia
julia> s = try
           sin("a")
       catch e
           io = IOBuffer()
           showerror(io, e)
           s = String(take!(io))
           split(s, '\n')[1]
       end
"MethodError: no method matching sin(::String)"

julia> startswith(s, "MethodError: no method matching sin")
true

```

This seems clunky and might break if the error string is changed at any point.

Is there a better way to achieve this?

---

<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, 2020, 11:04am UTC](https://discourse.julialang.org/t/what-is-the-best-future-proof-way-to-check-which-function-raised-a-methoderror/48354/2 "2020-10-16T11:04:59Z")

</div>

AFAIK errors have no public API other than the type hierarchy.

It is not clear to me why/how you would want to

> [@jishnub](#):
>
> ensure that the `MethodError` is raised by by a function in my package

I would recommend just aiming to get rid of _all_ errors in your package which are bugs, and just pass through the rest. Generally you don’t want to use errors for control flow.
