On Sep 24, 12:33 pm, Ron <ron.nata...@gmail.com> wrote:
On Sep 24, 5:02 am, Emil Berg <emilbe...@gmail.com> wrote:> Hello,
After reading some articles about exceptions, I still have
an unanswered question.
If I throw an exception as an object on the heap with this
way: throw new FileNotFoundException();
Then, is it possible to catch the exception as a reference
and delete the allocated exception object somehow from the
throwing site?
You're throwing a pointer. You catch the pointer and
delete it. However, why are you throwing a pointer? Why
not just throw the object itself:
throw FileNotFoundException();
Your exception objects should be relatively easy to copy by
design.
Never use dynamic allocation without an overriding reason.
You can catch the objects by reference if you're trying to
handle things polymorphicly.
What I want to do is pass exceptions between threads. I have
some exception classes that derive from CBaseException class.
Now I'm doing the following steps:
1. Throw FileNotFoundException on thread A
2. Catch it as CBaseException and save it in an CBaseException*
pointer.
3. Pass this pointer to thread B
4. Throw this exception manually.
5. Catching the exception on thread B as FileNotFoundException
reference
Steps 2,3,4 are part of a threading/messaging infrastructure
that I'm building, so I'd like to delete the exception on
thread B after throwing it (inside the infrastructure). I
don't want the user to delete the exception.
I hope that I explained it well and maybe you have a good idea
to solve it.
catch by const reference. Only at the outer layer of the thread
pointer).
static type).