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

**URL:** <https://discourse.julialang.org/t/catching-or-preventing-sizeof-exceptionerror-when-datatype-doesnt-have-a-specific-size/117983>\
**Category:** General Usage\
**Created:** [August 9, 2024, 3:05am UTC](https://discourse.julialang.org/t/catching-or-preventing-sizeof-exceptionerror-when-datatype-doesnt-have-a-specific-size/117983 "2024-08-09T03:05:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![zapiano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zapiano/32/48063_2.png) [@zapiano](https://discourse.julialang.org/u/zapiano)\
**Post date:** [August 9, 2024, 3:05am UTC](https://discourse.julialang.org/t/catching-or-preventing-sizeof-exceptionerror-when-datatype-doesnt-have-a-specific-size/117983/1 "2024-08-09T03:05:52Z")

</div>

When calling `sizeof(T::DataType)` with abstract types, an error is thrown (as mentioned here [Essentials · The Julia Language](https://docs.julialang.org/en/v1/base/base/#Base.sizeof-Tuple%7BType%7D)). 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)](https://github.com/JuliaDataCubes/YAXArrays.jl/issues/410).

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/](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…?

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [August 12, 2024, 4:07am UTC](https://discourse.julialang.org/t/catching-or-preventing-sizeof-exceptionerror-when-datatype-doesnt-have-a-specific-size/117983/2 "2024-08-12T04:07:44Z")

</div>

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.
