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

  • Home
  • SEARCH
  • 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 1091941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:36:34+00:00 2026-05-16T23:36:34+00:00

I’m implementing a Zip-Wrapper (zlib minizip) and asking myself how i should handle exceptions

  • 0

I’m implementing a Zip-Wrapper (zlib minizip) and asking myself how i should
handle exceptions properly. I’m thinking of three versions. Which one would
you prefer, or is there a version i didn’t thought about?

The task of the function Install is to get a Zip-File from a Web-Server,
unpack its content and delete the downloaded Zip-File. But if an error
occurs while unpacking the file, where should the Zip-File be deleted?

Tanks for your experiences.

Version A (with deleting outside the function):

void Install() {
    getFile("upd.zip"); // Creates File
    MyZip myzip("upd.zip");
    myzip.unzip();      // Can't do its job --> Exception
    delete("upd.zip");  // In case of exception: File would not be deleted here
}

int main() {
    try {
        Install();
    }
    catch (const Error& err) {
        delete("upd.zip"); // File must be deleted here
        MessageBox(err.text);
    }
}

Version B (with Re-Throwing the exception)

void Install() {
    getFile("upd.zip"); // Creates File
    try {
        MyZip myzip("upd.zip");
        myzip.unzip();
    }
    catch (const Error& err) {
        delete("upd.zip");
        throw err; // Re-Throw the Error
    }
    delete("upd.zip");
}

int main() {
    try {
        Install();
    }
    catch (const Error& err) {
        MessageBox(err.text);
    }
}

Version C (with return code)

void Install() {
    getFile("upd.zip"); // Creates File
    MyZip myzip("upd.zip");
    if (!myzip.unzip("upd.zip")) {
        delete("upd.zip");
        throw Error(myzip.geterror()); // what was the reason
    }
    delete("upd.zip");
}

int main() {
    // Same as in Version B
}
  • 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-05-16T23:36:34+00:00Added an answer on May 16, 2026 at 11:36 pm

    None of the three. Use RAII:

    class FileGuard {
    public:
        FileGurad(const std::string & filePath)
        : m_FilePath( filePath )
        {}
    
        ~FileGuard() {
            delete(m_FilePath); // must not throw an exception
        }
    private:
        std::string m_FilePath;
    };
    

    Usage:

    void Install() {
        guard FileGuard(getFile("upd.zip")); // Creates File; getFile should return the path where the file was created
        MyZip myzip("upd.zip");
        myzip.unzip();      // Can't do its job --> Exception
        // file will be deleted when guard goes out of scope (e.g. when an expection is thrown)
    }
    

    Alternatively you can have FileGuard call getFile in the constructor. See this answer (and others to the same question) for more information about stack unwinding (esp. the order of destruction).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.