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

Question about catch

P: n/a
int main()
{
try {
b();
} catch(...) {
return 1;
} catch(int i) {
return 2;
}

what ll happen to this code?
does it always generate syntax error because of the catch(...) phase?
or is it complier specific?

Thanks!

Dec 20 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
cw***@hotmail.com wrote in news:1166606607.464543.229260
@t46g2000cwa.googlegroups.com:
int main()
{
try {
b();
} catch(...) {
return 1;
} catch(int i) {
return 2;
}

what ll happen to this code?
does it always generate syntax error because of the catch(...) phase?
or is it complier specific?
The Standard (s15.3.6) says that the catch(...) "shall be the last handler
for its try block". (Oh, and I'm assuming that you have one more '}' to
close the main() block)
Dec 20 '06 #2

P: n/a
cw***@hotmail.com wrote:
int main()
{
try {
b();
} catch(...) {
return 1;
} catch(int i) {
return 2;
}
missing }
does it always generate syntax error because of the catch(...) phase?
or is it complier specific?
catch(...) must be last, it doesn't make sense any other way.
--
Ian Collins.
Dec 20 '06 #3

P: n/a
thanks for the quick reply!
yes, typo on the "}"

Dec 20 '06 #4

P: n/a
I think, it will not be compiler dependent.

Because at run time, we check all the catch blocks sequentially, and as
soon as we find datatype of exception and catch block are same , we
just ignore rest others.

And once catch(...) is found it means you will never check for other
catch blocks. So all compiler will be forcing to write catch(...) in
the last only.
cw***@hotmail.com wrote:
int main()
{
try {
b();
} catch(...) {
return 1;
} catch(int i) {
return 2;
}

what ll happen to this code?
does it always generate syntax error because of the catch(...) phase?
or is it complier specific?

Thanks!
Dec 20 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.