Re: Exception Semantics question
On Jun 24, 5:12 pm, "RB" <NoMail@NoSpam> wrote:
I have a question on try throw semantics. This has
nothing to do with whether I should try or catch but
just the fact that I don't understand what is going
on here. Admittedly this is not any real code but a
scenario that I happened on by accident in experimenting
with writing my own exception class. The below code
compiles and runs with no errors but a newb like me
doesn't understand how. See comment on GlobalFunc
#include <iostream.h>
class ClassNoObject
{
public:
ClassNoObject(){}
~ClassNoObject(){}
void ClsNoObjFunc()
{
cout << "no object class printing? \n";
}
};
void GlobalFunc()
{ // How can throw construct a class definition with
// no object, but seemingly does in the debugger ?
// What is happening here ?
cout << "fixing to throw ClassDefinition? \n";
throw ClassNoObject();
}
void main(void)
{
try
{
GlobalFunc();
}
// The E instead of * E appears it is passing a copy,
// but a copy of what object ?
catch( ClassNoObject E )
{
E.ClsNoObjFunc();
cout << "Caught exception\n";
}
}
Other people already explained a lot. I'll just add that in context of
MFC you should always use exception object derived from some MFC
exception class, you should throw them like so: throw new
CMyExceptionClass(params), and you should catch them like so:
catch(CMyException* pe). This is, sadly, different from "standard C++"
way of doing things. (see my other post).
Goran.