Copy constructor with Exception object

Started by
3 comments, last by Void 23 years, 7 months ago
I have a exception class which I would want to be catch by ref only, so I declare the copy constructor private. However, VC++ 6 refuses to compile, giving me an error an error that the exception object cannot be thrown because of an inaccessible copy constructor. But upon checking, the copy constructor is only called when the exception object is caught by value eg catch(MyExceptionClass e) // only this needs the copy constructor {} catch(MyExceptionClass &e) // this doesn''t need the copy constructor {} So has anyone been able to ensure the exception object is to be catch by ref only, not value??
Advertisement
quote:Original post by Void
So has anyone been able to ensure the exception object is to be catch by ref only, not value??


All objects that can be thrown need to have a copy constructor publicly available. It''s just one of those things you can''t really bypass. If you want to make sure the exception only has one copy around, throw a pointer instead of an actual exception object.

A hack, but it''s impossible to do what you want exactly.

MSN
A pointer?? Yucks!.. Maybe it can be wrapped up in a smart pointer??.. Juz a thought..

Thanks there!..
There was another post about this.
But here''s the deal.

When any object is thrown a copy must be made to throw.

This is because the local variable that holds the exception object will go out of scope when the exception is thrown.

Even pointer types will be copied - although you will only be copying the pointer rather than the object itself.

There is no way to enforce that an exception is caught be reference.

Unless you went with the ''throw the pointer only'' idea, but that will cause a memory leak because you will not know when to delete the object being pointed to - e.g. which catch handler deletes it?

Also, throwing a smart pointer ala auto_ptr may be possible since the copy ctor of auto_ptr is to transfer ownership of the pointer.

But I''m not so sure its worth it, plus you could still catch the auto_ptr by value.
quote:Original post by SteveC
There is no way to enforce that an exception is caught be reference.


This is real weird. Why would anyone want an exception to be caught be value?

Catching the exception by value will remove polymorphism on the exception object. So virtual functions called from the caught exception object will ALWAYS point to the base exception class.

Even the stdexcept suffers from this.. try throwing a logic_error and catching by using the base exception class. If the catch is by value, we lose all the hierarchy of the exception class (ie. class type and virtual function)

I''m just curious why it was designed as so..

    try{   throw logic_error("Logic error")}catch(exception e)   // catch by value, lose polymorphism{   typeid(e)       // will always point to exception class   e.what();       // will be empty}    

This topic is closed to new replies.

Advertisement