HDF5.jl icon indicating copy to clipboard operation
HDF5.jl copied to clipboard

`create_group` probably should throw a custom exception

Open ForceBru opened this issue 3 years ago • 3 comments

Right now there's a call to error:

https://github.com/JuliaIO/HDF5.jl/blob/ef228d57be54d2082a6a7581895fd4becbfe3714/src/HDF5.jl#L723

In my code, I'd like to overwrite the data in a group if it already exists, but currently that would make me catch a ErrorException:

try
    create_group(fid, grp_name)
catch e
   if e isa ErrorException
      @warn "Replacing data!"
   else
      throw(e)
   end
end

However, this doesn't tell me anything about where or why the ErrorException occurred: maybe the file was corrupt, maybe it got randomly deleted on disk - I can't tell.

It would be nice to have a type of exception that would explain what caused it and let me decide how to handle it. Something like this:

try
    create_group(fid, grp_name)
catch e
   if e isa GroupExistsException
      @warn "Replacing data!"
   else
      throw(e)
   end
end

ForceBru avatar Apr 18 '21 13:04 ForceBru

I think in your use case it would be better to first check the existence of the key, instead of relying on capturing an exception.

musm avatar Apr 19 '21 14:04 musm

I'm not opposed to creating and using a custom H5Exception <: Exception, I'm just not convinced we need to have custom exceptions for group errors e.g. H5GroupException seems too redundant.

musm avatar Apr 19 '21 14:04 musm

The manual error is a bit odd, as it isn't done for other functions (which typically return a H5Error), and it requires an extra haskey(parent, path) check.

simonbyrne avatar Jun 08 '22 05:06 simonbyrne