Re: What does this form of catch means?
Am 06.03.2014 14:48, schrieb Mirek Fidler:
today I have made a small typo in my code, adding parenthesis into
catch block:
struct exc {..};
try {
}
catch(exc()) { // instead of correct catch(exc)
}
To my surprise, compiler (MS) has reported no error (and on exception
program crashed, not catching the exception).
Then tried with g++, also compiled without errors, program also crashes.
Which left me totally surprised, because I have never heard about
putting expression instead of declaration into catch (or is that a bug
in both compilers).
I would appreciate any hints about what is going on here...
A tricky thing, this: An exception-declaration in a handler is quite
similar to a function declaration (but not equal!). The similarity goes
so far that this declaration behaves like a function that you had
declared like this:
void foo(exc());
The meaning of this is that the function parameter is declared as having
function type
exc()
[That is a function returning an exc without function parameters]
Now type adjustment happens to effectively produce a function type of
foo of:
void (exc(*)());
that is, the function type parameter becomes actually a function pointer
type. Essentially you are asking the handler that you expect a function
pointer
exc(*)()
But assuming you have thrown an object of type exc, this won't match
this declaration, therefore the runtime behaviour that you observe.
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]