Catching or preventing sizeof ExceptionError when DataType doesn't have a specific size

When calling sizeof(T::DataType) with abstract types, an error is thrown (as mentioned here Essentials · The Julia Language). But calling it with String also throws an error.

I have proposed a fix to a YAXArrays package issue. The issue happens when the user creates an YAXArray{T,D} and, inside this package, sizeof(T) is called. Because the DataType can be anything, sometimes it is AbstractType or a String and that causes the issue. Here’s the link to the issue Handling abstract types? · Issue #410 · JuliaDataCubes/YAXArrays.jl (github.com).

The thing is: because that doesn’t happen only with AbstractTypes, I can’t just check if isabstracttype(T) before calling sizeof. My approach was to catch the error, but it was pointed to me that this is not a good practice (https://discourse.julialang.org/t/please-stop-using-error-and-errorexception-in-packages-and-base/).

My questions are:

  1. Is there a way of checking if a DataType has a specific size before calling sizeof?
  2. Is there a better way of catching this error?
  3. Is there another good approach I am missing here…?

I mean the fix I propose in the issue of mine that is linked is to change the type of exception thrown in Base to throw something more specific than ErrorException.
Often via introducing a new exception type.
Which I have successfully done for other error types before.
But it honestly depends a lot on the particular reviewers your PR attracts.

Really though if you are catching it in a try catch that only contrains the sizeof command then you do know what is causing it.
There is an alternative (counter mainstream) physosphy to the one I propose that says one should infact catch (almost) all exceptions, because the only thing that matters is you know you can handle them appropriately, and if you scoped down what is in your try block narrowly enough, then there are few things that cause them that need to be handled differently since they can only come from one thing. Its counter the typical advice however.

1 Like