0

This excerpt is from Bjarne's book :

In principle, an exception is copied when it is thrown, so the handler gets hold of a copy of the original exception. In fact, an exception may be copied several times before it is caught. Consequently, we cannot throw an exception that cannot be copied.

If we catch an exception by reference or pointer then this doesn't hold true. Am I right? Does author here means that thrown object is first copied internally and that copy is passed to the handler argument, where that will again be copied if the handler argument is pass by value

alexisdm
  • 29,448
  • 6
  • 64
  • 99
JamesWebbTelescopeAlien
  • 3,547
  • 2
  • 30
  • 51
  • A related question [How are exceptions allocated on the stack caught beyond their scope](http://stackoverflow.com/questions/2404288/how-are-exceptions-allocated-on-the-stack-caught-beyond-their-scope) – Bo Persson Apr 14 '12 at 23:52

1 Answers1

2

Regardless of if the exception is caught by value or reference it is still a copy of the object. I can recomend reading Meyers, More Effective C++, Item 12 "Understand how throwing an exception differs from passing a parameter or calling a virtual function" which throws some light on this.
Even if the catch handler catches by references the reference must be a copy because the original object may have been destroyed.
Note however that catch by pointer does NOT involve a copy - but the thrower is responsible for ensuring that the exception the catcher gets a pointer to has not been destroyed. Catching by pointer is not recomended. Which is just one reason Meyers recomends in general to always catch by reference see Meyers, Item 13 "Catch exceptions by reference" for others!

Ricibob
  • 7,505
  • 5
  • 46
  • 65