By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,986 Members | 2,086 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,986 IT Pros & Developers. It's quick & easy.

catch with no explicit object

P: n/a
class excep
{

};

int main ( )
{
try
{
throw excep ();
}
catch (excep)
{

}
}

Is the catch (excep) equivalent to
catch (excep& e) or
catch (excep e)

Also is it possible to get an handle to the excep object in
catch(excep)

Sep 22 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Ninan wrote:
class excep
{

};

int main ( )
{
try
{
throw excep ();
}
catch (excep)
{

}
}

Is the catch (excep) equivalent to
catch (excep& e) or
catch (excep e)
Do you see the ampersand there? So, why ask? Is 'foo(int)'
equivalent to 'foo(int& a)' or 'foo(int a)'?
Also is it possible to get an handle to the excep object in
catch(excep)
Yes. Name that object and use the name.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 22 '06 #2

P: n/a
Ninan wrote:
class excep
{

};

int main ( )
{
try
{
throw excep ();
}
catch (excep)
{

}
}

Is the catch (excep) equivalent to
catch (excep& e) or
catch (excep e)
It is not equivalent to either of them, but it is nearly so to the
latter. The difference is that the latter creates a local copy of the
exception that you can access. It's like if you call a function but
don't name a parameter:

void Foo( int ) { /*...*/ }

Foo() can only be called with an int parameter (or something implicitly
convertible to an int), but within Foo, the programmer has no access to
what the int value was.
Also is it possible to get an handle to the excep object in
catch(excep)
See above. But you should generally catch by *const reference*. See
these FAQs:

http://www.parashift.com/c++-faq-lit....html#faq-17.7
http://www.parashift.com/c++-faq-lit....html#faq-18.1

Cheers! --M

Sep 22 '06 #3

P: n/a
Victor Bazarov wrote:
Ninan wrote:
class excep { };

int main ( ) {
try {
throw excep ();
}
catch (excep) {
}
}
Also is it possible to get an handle to the excep object in
catch(excep)

Yes. Name that object and use the name.
Well, that wasn't really the solution he was looking for I guess. It
must be
the last created excep object. So add a global excep* lastinstance and
set that in excep::excep(excep const&). Then you only need to get
*lastinstance. I mean, he wouldn't asked for the obvious solution if
that would work, now would he?

Michiel.

Sep 22 '06 #4

P: n/a
Mi*************@tomtom.com wrote:
Victor Bazarov wrote:
>Ninan wrote:
>>class excep { };

int main ( ) {
try {
throw excep ();
}
catch (excep) {
}
}
>>Also is it possible to get an handle to the excep object in
catch(excep)

Yes. Name that object and use the name.

Well, that wasn't really the solution he was looking for I guess. It
must be
the last created excep object.
Last created by whom? By a destructor of some object being disposed
of during stack unwinding? Nobody prevents me from saying

excep *myexcep = new excep;

anywhere. It doesn't cause stack unwinding to fail.
So add a global excep* lastinstance
How many global data does your code usually have, per a kiloLOC?
Just curious, I guess.
and
set that in excep::excep(excep const&). Then you only need to get
*lastinstance. I mean, he wouldn't asked for the obvious solution if
that would work, now would he?
Don't put words into somebody else's mouth, don't assume people know
what they are doing, don't assume if something's obvious to you, it is
to everybody.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 22 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.