From: RB on
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";
}
}

From: Bo Persson on
RB 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();

This is the construction of a new object, which is then thrown. There
is now 'new' here, so it is not a pointer.

> }
>
> 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";
> }
> }

Right, you catch the exception object by value. Quite ok.


Bo Persson


From: RB on

>> cout << "fixing to throw ClassDefinition? \n";
>> throw ClassNoObject( );
>
> This is the construction of a new object, which is then thrown.
> There is now 'new' here, so it is not a pointer.

Thanks for the reply but I am still confused. ClassNoObject is
only a declared class definition, it is not a object. How does
throw create an object of this ??
In other words I could not just
ClassNoObject.ClsNoObjFunc( );
or
ClassNoObject::ClsNoObjFunc( );
since these would pull compiler errors.
What is the deal with throw ?


From: Thomas J. Gritzan on
Am 24.06.2010 23:00, schrieb RB:
>
>>> cout << "fixing to throw ClassDefinition? \n";
>>> throw ClassNoObject( );
>>
>> This is the construction of a new object, which is then thrown. There
>> is now 'new' here, so it is not a pointer.
>
> Thanks for the reply but I am still confused. ClassNoObject is
> only a declared class definition, it is not a object. How does
> throw create an object of this ??
> In other words I could not just
> ClassNoObject.ClsNoObjFunc( );
> or
> ClassNoObject::ClsNoObjFunc( );
> since these would pull compiler errors. What is the deal with throw ?

It has nothing to do with throw.
ClassNoObject() creates a temporary of this class, just as int() creates
a temporary int.

You can also do, for example:

// returns a copy of this temporary
return ClassNoObject();

// creates a temporary string containing "foo" and returns a copy of it
return std::string("foo");

The same works for throw.

--
Thomas
From: Giovanni Dicanio on
On 24/06/2010 18:22, Bo Persson wrote:

>> catch( ClassNoObject E )
>> {
>> E.ClsNoObjFunc();
>> cout<< "Caught exception\n";
>> }
>> }
>
> Right, you catch the exception object by value. Quite ok.

But I used to think that the correct way to catch exceptions is by
(const) reference:

catch (SomeException & e)

or

catch (const SomeException & e)


Giovanni