catching exceptions

  • Thread starter Samuele Armondi
  • Start date
S

Samuele Armondi

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
 
V

Victor Bazarov

Samuele Armondi said:
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
 
S

Samuele Armondi

Victor Bazarov said:
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!
 
R

Ron Natalie

Samuele Armondi said:
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) { ...
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top