Oct 10, 2010 at 3:49pm UTC
Hello, I can't compile this code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#define CATCH_EXCEPTIONS(c)\
try{ \
##c ; \
} \
catch(...){ \
MessageBox(0,"blah blah","Runtime exception",64); \
}
void fx()
{
throw 112;
}
int main()
{
CATCH_EXCEPTIONS(fx());
}
The CATCH_EXCEPTIONS macro should catch exceptions when it is thrown but I get a compiler error:
pasting "{" and "fx" does not give a valid preprocessing token|
How can I fix this?
Thanks for help!
Last edited on Oct 10, 2010 at 3:50pm UTC
Oct 10, 2010 at 3:52pm UTC
Delete the
## on line 4. ## takes a macro argument on the left and right, and it smushes them together with no spaces in-between.
http://www.cplusplus.com/doc/tutorial/preprocessor/
2 3 4 5 6 7 8
#define CATCH_EXCEPTIONS(c)\
try{\
c;\
}\
catch(...){\
MessageBox(0,"blah blah","Runtime exception",MB_OK);\
}
Last edited on Oct 10, 2010 at 3:57pm UTC
Oct 10, 2010 at 4:00pm UTC
Oh, such a stupid mistake!
Thanks!
Oct 10, 2010 at 6:56pm UTC
Not a great use of macros, I must say.
For one thing, macros should not be used to invent new grammar, which is sort of what you're doing.
And secondly, if you have try-catch blocks littered throughout your code (which prompted creating
the shorthand in the first place) then you are probably misusing exceptions.