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 7820335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:15:28+00:00 2026-06-02T07:15:28+00:00

I am using QT 4.8 (C++) for desktop application project, and writing exception handling

  • 0

I am using QT 4.8 (C++) for desktop application project, and writing exception handling which is as follows :

void callerMethod()
{
  try
  {
   method1();
  }
  catch(Exception1& e)
  {
    // display critcal error message
    // abort application
  }
  catch(std::Exception& e)
  {
   // print exception error message
  }
  catch(...)
  {
   // print unknown exception message
  } 
}

void method1()
{
  try
  {
   // some initializations
   // some operations (here exceptions can occur)
   // clean-up code (for successful operation i.e no exception occurred)
  }
  catch(Exception1& e)
  {
   // clean-up code
   throw e;
  }
  catch(Exception2& e)
  {
   // clean-up code
   throw e;
  }
  catch(Exception3& e)
  {
   // clean-up code
   throw e;
  }
  catch(...)
  {
   // clean-up code
   throw;
  }
}

So my question do I need to write the clean-up code in every catch block?
Is there any way I can avoid writing repeated code?

NOTE:: [ In method1() ] I want to re-throw exceptions which occurred
to my caller.So I can not catch them in single catch block,
because then type information will be lost.

  • 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-02T07:15:30+00:00Added an answer on June 2, 2026 at 7:15 am

    Method1 can be much simplified by two concepts:

    1. RAII. Put any clean-up code into destructors, and the clean-up code will be centralized.
    2. Use the unqualified throw, and you won’t need to know about the type of exception thrown.

    So, method1() should look like:

    void method1()
    {
         // some initializations of RAII objects
         // some operations (here exceptions can occur)
    }
    

    The first catch clause in callerMethod can be removed if you derive Exception1 from std::exception, since the what() method is virtual.

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

Sidebar

Related Questions

We've developed a desktop application using .NetFX3.5 which has some winforms and two WPF
I m trying to create a desktop application using swing in java, which allows
I'm working on a school project desktop application in c# using Visual Studio 2010
As part of my Final Year Project I've developed a desktop application, which fits
For a pet project, I develop a desktop application which requires API keys from
I have developed a small desktop application in c#(using windows forms). For this project
Currently i am working on a desktop application which consists mathematical analysiss.I am using
I have to develop a desktop application using Java. I've some experience with Java
Hey I am developing an desktop application using Spring and Hibernate, and I have
I'm building a centralized desktop application using Python/wxPython. One of the requirements is User

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.