Re: Exception handling?
On Jun 23, 2:54 pm, "RB" <NoMail@NoSpam> wrote:
Hey thanks for replying, this thread picked up while your timezone
was sleeping. I will respond to this one first and then any other
replies you sent to me if not material already covered here.
As I said: you are not permitted to write try/catch statements ;-)
Yes I do remember you saying that but at the time I did not really
understand what you meant by it other than the fact that most of the
time it would end up being counterproductive or something to that
effect. I get the most from your replies after I experiment thru a
few foobar attempts and then go back and read them again.
I also remember you saying,
Find the answer to that, and you'll find that you don't need a try/catc=
h
And I have found the answer to that.
But on your code example,
try
{
workworkwork();
}
catch(CException* p)
{
DEL_ON_EXIT(p);
p->ReportError();
}
I see what calling ReportError does. When I tried it in my code
it just called reported the error but did no cleanup etc, so obviously
you were obviously explaining " in relation to " here.
However I would like to express these couple of items here
since I am learning LOADS from all of this.
First off exactly what is the define DEL_ON_EXIT ? My VC6
compiler doesn't reconize it so I need the include or whatever ?
Ugh. Can't you get a more recent compiler? VC6 - ugh!
About DEL_ON_EXIT: I made it for myself a long time ago, when I
decided that I don't want to meddle with MFC exception macros anymore.
Here it is:
class CMFCExceptionDelete // make it noncopyable
{
public:
CMFCExceptionDelete(CException* pe) : m_pe(pe) {} // should be
explicit...
~CMFCExceptionDelete() { m_pe->Delete(); }
private:
CException* m_pe;
void operator=(const CMFCExceptionDelete&) {}
CMFCExceptionDelete(const CMFCExceptionDelete&) {}
};
#define DEL_ON_EXIT(e) CMFCExceptionDelete Delete_e_OnExit(e);
How to use:
....
catch(CException* pe)
{
DEL_ON_EXIT(pe); // Always a first thing to do in a catch!
// do anything you like, including throwSomeOtherException
// don't do throw; or throw pe;
}
That ensures that pe is alive inside catch and that it's pe->Delete()-
d at block exit.
Drawback: you can't hold on to pe after the block exit. If you need
that, find something else ;-).
Goran.