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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:41:50+00:00 2026-05-11T07:41:50+00:00

There is a try-catch thing about functions, which I think sometimes may be quite

  • 0

There is a try-catch thing about functions, which I think sometimes may be quite useful:

bool function()  try {     //do something } catch(exception_type & t) {    //do something } 

So the first part of the question: is this style considered bad in general case?

And the concrete example I used this approach in:

We had project with quite a lot of code in c and c++. And there we had custom exception types (not std::exception derived). I needed to integrate XML library and cast all exception to our types. So, basically, the last step was to catch all exceptions from XML library and convert them.

Function before:

bool readEntity(...) {    while(...)    {        if(...)       {          //lot's of code...       }    } } 

after:

bool readEntity(...) try {    while(...)    {        if(...)       {          //lot's of code...       }    } } catch(XMLBaseException & ex) {     //create our exception and throw } 

My thoughts went something like this: I clearly state my intentions to convert all exception derived from one type into custom type AND we keep our screen without horizontal scroll bar (cause horizontal scroll bars are bad).

Well, I actually was quite criticized for this approach as for non-clear one during code review.

So I’d like to hear you thoughts.

UPDATE: just to be clear: refactoring the function wasn’t an option. And actually it was good written one.

  • 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. 2026-05-11T07:41:51+00:00Added an answer on May 11, 2026 at 7:41 am

    Really the only reason to function-level try blocks is for constructors, otherwise it’s a somewhat obscure feature that doesn’t buy you that much. It’s just as easy to do it this way:

    bool readEntity(...) {    try    {       while(...)       {           if(...)          {             //lot's of code...          }       }    }    catch(XMLBaseException & ex)    {        //create our exception and throw    } } 

    If you are having troubles with horizontal scrolling then the thing to do is to split up your code. try/catches are complexity and this should be represented in the nesting level, not hidden.

    In constructors, this is a different issue: there is no other way to catch exceptions in an initializer list:

    SomeClass::SomeClass(parameter p1, parameter p2) : Member1(p1), Member2(p2) try {  } catch(Exception &ex) {     // handle and rethrow } 

    Of course, if you have an exception mid-construction, there’s not likely much you can do to recover except log and and rethrow (it’s going to get rethrown anyway in the constructor case). Your object isn’t completely constructed yet and there’s nothing you can really do with it. The only thing that you can trust to be valid are the parameters (although if the initialization failed, that will likely be due to bad parameters).

    See this GOTW for a discussion on this.

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

Sidebar

Related Questions

I've been thinking about nested try/catch statements and started to think about under which
Short of inserting a try/catch block in each worker thread method, is there a
What kind of performance implications are there to consider when using try-catch statements in
There's a lot of conflicting information about this topic. So let's try to agree
I might be missing something obvious but is there a reference somewhere about what
Is there a way to test if a collection is already initialized? try-catch only?
I'm using Instruments to try to determine if there are places in my application
According to the Java Language Specification : If there are any enclosing try statements
I have the following javascript code, which loads without error, however the update function
I have implemented this function: static <X,Y> Y castOrNull(X obj) { try { return

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.