# Differences between extending \`Base.show\` and \`Base.showerror\` for a custom exception type in Julia?

**URL:** <https://discourse.julialang.org/t/differences-between-extending-base-show-and-base-showerror-for-a-custom-exception-type-in-julia/101003>\
**Category:** General Usage\
**Tags:** question, error, error-message\
**Created:** [June 29, 2023, 11:09pm UTC](https://discourse.julialang.org/t/differences-between-extending-base-show-and-base-showerror-for-a-custom-exception-type-in-julia/101003 "2023-06-29T23:09:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)\
**Post date:** [June 29, 2023, 11:09pm UTC](https://discourse.julialang.org/t/differences-between-extending-base-show-and-base-showerror-for-a-custom-exception-type-in-julia/101003/1 "2023-06-29T23:09:38Z")

</div>

I’m working on a Julia project where I have defined a custom exception type like this:

```julia
struct XXXError <: Exception
    e::String
end

```

Now, I want to customize how instances of this exception type are displayed. I understand that I can achieve this by extending either the `Base.show` or `Base.showerror` function. However, I’m not entirely sure about the differences between these two functions and in which situations I should use each.

From what I understand, `Base.show` is a general function used to display objects in Julia, while `Base.showerror` is specifically used to display error messages. Is my understanding correct? I see most `Exception` types just define `Base.showerror`.

Could someone please explain the key differences between extending `Base.show` and `Base.showerror` for a custom exception type? And could you also provide guidance on when to use each? Thank you.

---

<div class="post-metadata">

**Author:** ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)\
**Post date:** [June 29, 2023, 11:14pm UTC](https://discourse.julialang.org/t/differences-between-extending-base-show-and-base-showerror-for-a-custom-exception-type-in-julia/101003/2 "2023-06-29T23:14:08Z")

</div>

From the example in the documentation, I kind of get it:

```julia
julia> struct MyException <: Exception
           msg::String
       end

julia> function Base.showerror(io::IO, err::MyException)
           print(io, "MyException: ")
           print(io, err.msg)
       end

julia> err = MyException("test exception")
MyException("test exception")

julia> sprint(showerror, err)
"MyException: test exception"

julia> throw(MyException("test exception"))
ERROR: MyException: test exception

```

```julia
julia> err = MyException("test exception")
MyException("test exception")

```

This line implicitly calls `show` and is equivalent to `show(err)`:

```julia
julia> show(err)
MyException("test exception")

```

while `showerror` is only used when `throw` is called. Is it correct?
