# Handling errors when overwriting Base.show

**URL:** <https://discourse.julialang.org/t/handling-errors-when-overwriting-base-show/17935>\
**Category:** Juno\
**Created:** [November 24, 2018, 3:19am UTC](https://discourse.julialang.org/t/handling-errors-when-overwriting-base-show/17935 "2018-11-24T03:19:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tlienart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlienart/32/7640_2.png) [@tlienart](https://discourse.julialang.org/u/tlienart)\
**Post date:** [November 24, 2018, 3:19am UTC](https://discourse.julialang.org/t/handling-errors-when-overwriting-base-show/17935/1 "2018-11-24T03:19:45Z")

</div>

Hello,  
How does one handle errors thrown in a `Base.show` overwrite so that they’re displayed in the REPL and not like this:

 ![00](https://global.discourse-cdn.com/julialang/original/3X/3/4/34bc8bda0ae63ce0da8e59852653ec8838b0961d.png)

this is generated from a file with

```julia
struct Foo
    fn::String
end

function Base.show(io::IO, ::MIME"image/png", f::Foo)
    isfile(f.fn) || error("File not found")
    write(io, read(f.fn))
end

Foo("mario.png") # works as expected
Foo("mario_notfound.png") # leads to the error above, I'd like that error to be in the REPL

```

Interestingly if I copy paste the exact same code in the REPL itself, the error shows in the REPL (though the plot pane still opens which I’d like to avoid).

So to summarise:

- Is there a proper way to collect & display errors that may arise in the application of a `Base.show` so that they are shown in the REPL and not “on the side”
- Is there a way to not trigger the opening of the plot-pane if the `Base.show` fails?

thanks!

PS: if it seems weird to have possible errors in a Base.show and not earlier, the reason is that I’d like `Foo` to be a container for information to be passed to an external program which then generates the PNG, I want this to only be triggered when necessary and so not a-priori which is why the run command would happen in the `Base.show`. But since it may fail, I’d like to know how to do proper error handling.

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [November 24, 2018, 9:11am UTC](https://discourse.julialang.org/t/handling-errors-when-overwriting-base-show/17935/2 "2018-11-24T09:11:39Z")

</div>

> [@tlienart](#):
>
> Is there a proper way to collect & display errors that may arise in the application of a `Base.show` so that they are shown in the REPL and not “on the side”

Not for you, no. This needs to be addressed in Juno itself. Feel free to open an issue at the Juno.jl repo and I’ll look into it.

> [@tlienart](#):
>
> Is there a way to not trigger the opening of the plot-pane if the `Base.show` fails?

No, we need to open the plot pane before a `show` method is called to get the panes size and pass that to `show`. Arguably we should only do that for `application/prs.juno.plotpane+html` and not for more standard mime types, but e.g. Plots.jl’s Juno integration expects that information for normal mime types as well.

---

<div class="post-metadata">

**Author:** ![tlienart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlienart/32/7640_2.png) [@tlienart](https://discourse.julialang.org/u/tlienart)\
**Post date:** [November 25, 2018, 12:14am UTC](https://discourse.julialang.org/t/handling-errors-when-overwriting-base-show/17935/3 "2018-11-25T00:14:07Z")

</div>

Ok, very clear. I’ll open an issue for the first one. Thanks Sebastian!
