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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:02:41+00:00 2026-05-25T13:02:41+00:00

When catching an exception the standard guidance is to throw by value, catch by

  • 0

When catching an exception the standard guidance is to throw by value, catch by reference. As I understand it, this is for two reasons:

  1. If the exception was thrown due to an out of memory exception, we won’t call a copy constructor which could potentially terminate the program.
  2. If the exception is part of an inheritance heirarchy, we may potentially have object slicing on the exception.

If we have a scenario where we don’t define the exception name in the catch block are these concerns (really 1., as slicing won’t be an issue if we don’t have a name for the variable) still valid?

For example:

catch(my_exception)
{ ... }

or

catch(my_exception &)
{ ... }

Is there still the possibility of the program terminating if the exception caught by value in this case? My feeling is that it is technically still possible.

Note: I am asking this because I have had to review someone’s code who put a catch by value in this case. As shown in the question I am not entirely sure on the technical impact of either choice, but I think that in terms of consistancy it is better to catch by reference in this case regardless (there is no downside to catching by reference in any case).

  • 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-25T13:02:42+00:00Added an answer on May 25, 2026 at 1:02 pm

    The standard does not require special optimization in the case of an unnamed exception object. On the contrary, it then requires an effect as if a temporary is copy-initialized. This copying can result in memory being dynamically allocated.

    N3290 §15.3/16:
    If the exception-declaration specifies a name, it declares a variable which is copy-initialized (8.5) from the
    exception object. If the exception-declaration denotes an object type but does not specify a name, a temporary (12.2) is copy-initialized (8.5) from the exception object. The lifetime of the variable or temporary
    ends when the handler exits, after the destruction of any automatic objects initialized within the handler.

    The paragraph above does not mention catching by reference, and so one might reasonably conclude that it applies whether or not the exception object is caught by reference; that a copy is constructed anyway.

    However, that is contradicted by the next paragraph:

    N3290 §15.3/17:
    When the handler declares a non-constant object, any changes to that object will not affect the temporary
    object that was initialized by execution of the throw-expression. When the handler declares a reference to
    a non-constant object, any changes to the referenced object are changes to the temporary object initialized
    when the throw-expression was executed and will have effect should that object be rethrown.

    So, declared type T& (with T non-const) is the single case where C++11 requires a reference directly to the thrown object, instead of copying. And it is also that way in C++03, except that C++03 has some additional wording about as-if optimization. So, for the formal the preference should be for

        catch( T& name )
    

    and

        catch( T& )
    

    However, I have always caught exceptions like catch( T const& ). From a practical point of view one may assume that the compiler will optimize that to a direct reference to the thrown object, even though it is possible to devise cases where the observed program effect would then be non-standard. For example <evil grin>

    #include <stdio.h>
    #include <stdexcept>
    
    struct Error
        : std::runtime_error
    {
    public:
        static Error* pThrown;
    
        char const* pMessage_;
        Error()
            : std::runtime_error( "Base class message" )
            , pMessage_( "Original message." )
        {
            printf( "Default-construction of Error object.\n" );
            pThrown = this;
        }
    
        Error( Error const& other )
            : std::runtime_error( other )
            , pMessage_( other.pMessage_ )
        {
            printf( "Copy-construction of Error obejct.\n" );
        }
    
        char const* what() const throw() { return pMessage_; }
    };
    
    Error*  Error::pThrown  = 0;
    
    int main()
    {
        printf( "Testing non-const ref:\n" );
        try
        {
            throw Error();
        }
        catch( Error& x )
        {
            Error::pThrown->pMessage_ = "Modified message.";
            printf( "%s\n", x.what() );
        }
    
        printf( "\n" );
        printf( "Testing const ref:\n" );
        try
        {
            throw Error();
        }
        catch( Error const& x )
        {
            Error::pThrown->pMessage_ = "Modified message";
            printf( "%s\n", x.what() );
        }
    }
    

    With both MinGW g++ 4.4.1 and Visual C++ 10.0 the output is …

    Testing non-const ref:
    Default-construction of Error object.
    Modified message.
    
    Testing const ref:
    Default-construction of Error object.
    Modified message
    

    A pedantic formalist might say that both compilers are non-conforming, failing to create a copy for the Error const& case. A purely practical practitioner might say that hey, what else did you expect? And me, I say that the wording in the standard is very far from perfect here, and that if anything, one should expect a clarification to explicitly allow the output above, so that also catching by reference to const is both safe and maximally efficient.

    Summing up wrt. the OP’s question:

    • Catching by reference won’t call a copy constructor which could potentially terminate the program.

    • The standard only guarantees this for reference to non-const.

    • In practice, as shown, it is also guaranteed for reference to const, even when program results are then affected.

    Cheers & hth.,

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

Sidebar

Related Questions

I would much prefer to do this without catching an exception in LoadXml() and
Are there any negatives to catching an exception and just logging it? This is
I'm using the following code to unzip files. I'm catching this exception unZip()=java.io.IOException, but
this is a continuation of question Catching an exception while using a Python 'with'
Is catching an exception thrown in a loaded shared library portable? I've noticed that
Catching an exception that would print like this: Traceback (most recent call last): File
We have exception catching code in most of our event handlers etc, this leads
I am using the exception catching procedure which is the module to track the
I have a problem in catching an exception. I am trying to rethrow an
I tried two ways of catching unexpected unhandled exceptions: static void Main() { AppDomain.CurrentDomain.UnhandledException

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.