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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:02:27+00:00 2026-06-01T21:02:27+00:00

I currently have some code I am trying to refactor. A large set of

  • 0

I currently have some code I am trying to refactor. A large set of exceptions has some common code for all exceptions as well as some specific code which needs to be handled separately for each specific exception type. I am trying to figure out how to get rid of the common part in each catch block. One Idea is to do it like this:

try {
  /* Stuff that may fail */
} catch( const std::exception & ) {
  /* do common part here */
  try { throw; } 
  catch( const exception1 & ) {
    /* do stuff for exception1 here */
  }
  catch( const exception2 & ) {
    /* do stuff for exception2 here */
  }
}

However this strikes me as somewhat ugly.

Is there a better way to factor out this common logic, or is there actually a reason to avoid this attempt altogether?

  • 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-01T21:02:28+00:00Added an answer on June 1, 2026 at 9:02 pm

    In general, try/catch should only appear sparsely in the code. The problem is that many times the actions done in a catch clause should also be done in case of early return, for example.

    Idiomatic C++ uses RAII extensively to avoid situation where you need to cleanup in a catch clause, which generally removes most of the work.

    Now, your pattern is not so bad per se, it does factor the common stuff. But this common stuff could perhaps be handled automatically.

    In all the code bases I have stumbled upon, only a few times did I see a genuine use of catch clause, don’t use it as a clutch.

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

Sidebar

Related Questions

I currently have some code which when you press the submit button, a marker
I've been trying to implement unit testing and currently have some code that does
I currently have some code which iv used to export a table from the
I'm using Zend_Search_Lucene, the PHP port of Java Lucene. I currently have some code
I have some code that currently checks for minimum and maximum lentgh. I want
Currently, I have some code as follows template<typename Type> Type* getValue(std::string name, bool tryUseGetter
I have some JSF code that currently works (as shown below), and I need
I have inherited some code which involves a scheduled task that writes data (obtained
I need to abstract some behavioural code and have a problem trying to reference
We currently have some php code that allows an end user to update records

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.