Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8981283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:17:50+00:00 2026-06-15T20:17:50+00:00

I have been trying to come up with a way to get around some

  • 0

I have been trying to come up with a way to get around some of the shortcomings of the HDF5 C++ bindings. Currently, my code is littered with try/catch blocks similar to the following:

H5::Exception::dontPrint();
H5::H5File *file = NULL;
try {
    file = new H5::H5File(fname.c_str(), H5F_ACC_RDWR);
} catch(H5::FileIException &file_exists_err) {
    file = new H5::H5File(fname.c_str(), H5F_ACC_TRUNC);
}

This shouldn’t be necessary – all I want to do is to open a file for read/write access, and if it doesn’t exist, to create it. Another, trickier problem is to create a nested group (e.g. “/parent/group”), where the parent group does not necessarily exist. In Unix/Linux, the equivalent would be

mkdir -p parent/group

However, in the HDF5 C++ bindings, creating a group whose parent group does not exist raises an exception.

For these reasons, I have been motivated to create a header file which deals with some of these common problems. My first thought was to simply create a set of functions that would, for example, take a filename and access mode and return an H5::H5File object, or take a group name and return a group object. I think this is less than ideal, however, as it leaves the programmer who uses this header file to call “delete” on the returned objects, even though the programmer never explicitly calls “new” in their own code. This seems to be asking for memory leaks.

My second thought, therefore, was to create a set of derived classes from H5::H5File and H5::H5Group, with constructors that do not throw exceptions when the file does not yet exist, or when the group’s parent group does not yet exist. My attempt for the derived file class was as follows:

namespace H5Utils {

class H5File : public H5::H5File {
public:
    H5File(std::string fname);
    ~H5File();
};

}

H5Utils::H5File::H5File(std::string fname)
try : H5::H5File(fname.c_str(), H5F_ACC_RDWR)
{
    std::cerr << "Opened existing file." << std::endl;
} catch(H5::FileIException &file_exists_err) {
    std::cerr << "File does not exist. Creating new file." << std::endl;
    H5::H5File(fname.c_str(), H5F_ACC_TRUNC);
}

H5Utils::H5File::~H5File() { }

The problem that I am running into is twofold. First, the try/catch block in the constructor re-throws the exception created by

H5::H5File(fname.c_str(), H5F_ACC_RDWR)

when the file does not exist, so program still terminates. The second problem is that I am not sure that the second constructor,

H5::H5File(fname.c_str(), H5F_ACC_TRUNC);

is correct (i.e. does it construct the parent class?) Is there a way to have the derived class catch exceptions in the base class’ constructor, and then call a different constructor for the base class?

More generally, can anyone think of a better/more elegant way of dealing with these shortcomings of the HDF5 C++ bindings?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T20:17:52+00:00Added an answer on June 15, 2026 at 8:17 pm

    I prefer your initial idea of create some simple helper functions – it will be simpler and minimise the amount of code you’ll have to write and document. Additionally, to ensure proper memory management, you can use shared_ptr.

    Here’s a simple wrapper function equivalent to your initial example:

    // a typedef for our managed H5File pointer
    typedef std::shared_ptr<H5::H5File> H5FilePtr;
    
    // create or open a file
    H5FilePtr create_or_open(const std::string& fname)
    {
        H5::Exception::dontPrint();
        H5::H5File* file = 0;
    
        try {
            file = new H5::H5File(fname.c_str(), H5F_ACC_RDWR);
        } catch(const H5::FileIException&) {
            file = new H5::H5File(fname.c_str(), H5F_ACC_TRUNC);
        }
    
        return H5FilePtr(file);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying some programs in the C Language and come across to
Background: I have been charged with trying to come up with a new way
I've been trying to get my head around nginx rewrites today and have read
I have been struggling for a few days trying to find out how come
I have been trying to setup git for our web development team unsuccessfully. Some
Using jQuery and jQuery.Colors, I've been trying to come up with a way to
I have been wrestling with SvcUtil all day, trying to get it to generate
I've been trying to wrap my head around a good way to do this,
I have been trying to get a number from a html string, but I
I have been banging my head against the wall trying to get Exscript installed.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.