catching exceptions

Discussion in 'C++' started by Samuele Armondi, Jul 6, 2003.

  1. Hi everyone,
    I vaguely recall someone mentioning that exceptions should be caught as
    const in order to avoid unnecessary copying, i.e.
    try
    {
    // blah
    }
    catch (const exception e)
    {
    //deal with it
    }
    Is this the case? If so, why does const stop the object from being copied?
    Thanks in advance,
    S. Armondi

    --
    To contact me by email, remove _NOSPAM_ from the address.
    Samuele Armondi, Jul 6, 2003
    #1
    1. Advertising

  2. "Samuele Armondi" <> wrote...
    > I vaguely recall someone mentioning that exceptions should be caught as
    > const in order to avoid unnecessary copying, i.e.
    > try
    > {
    > // blah
    > }
    > catch (const exception e)
    > {
    > //deal with it
    > }
    > Is this the case? If so, why does const stop the object from being copied?


    It doesn't. You probably want them to be caught by reference:

    catch (const exception & e)
    {
    // deal with it
    }

    Victor
    Victor Bazarov, Jul 7, 2003
    #2
    1. Advertising

  3. "Victor Bazarov" <> wrote in message
    news:...
    > "Samuele Armondi" <> wrote...
    > > I vaguely recall someone mentioning that exceptions should be caught as
    > > const in order to avoid unnecessary copying, i.e.
    > > try
    > > {
    > > // blah
    > > }
    > > catch (const exception e)
    > > {
    > > //deal with it
    > > }
    > > Is this the case? If so, why does const stop the object from being

    copied?
    >
    > It doesn't. You probably want them to be caught by reference:
    >
    > catch (const exception & e)
    > {
    > // deal with it
    > }
    >
    > Victor
    >
    >

    Ok, thanks for the info. I probably just misread it!
    Samuele Armondi, Jul 7, 2003
    #3
  4. Samuele Armondi

    Ron Natalie Guest

    "Samuele Armondi" <> wrote in message news:3f09ecfa$...
    > Hi everyone,
    > I vaguely recall someone mentioning that exceptions should be caught as
    > const in order to avoid unnecessary copying, i.e.
    > try
    > {
    > // blah
    > }
    > catch (const exception e)
    > {
    > //deal with it
    > }


    Not const, but reference (which may also be const).

    catch(exception& e) { ...
    Ron Natalie, Jul 8, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marina
    Replies:
    2
    Views:
    475
    Marina
    Jul 8, 2003
  2. Amil Hanish
    Replies:
    0
    Views:
    550
    Amil Hanish
    Apr 13, 2006
  3. Adam Maass
    Replies:
    5
    Views:
    402
    Sudsy
    Jul 22, 2003
  4. Mike Schilling
    Replies:
    2
    Views:
    351
    Mike Schilling
    Jul 16, 2003
  5. Mick
    Replies:
    0
    Views:
    430
Loading...

Share This Page