Reputation: 2406
May be a dumb question, but I would like to use C++ exceptions in a console app (created with the new Win32 Console Application project wizard). I tried many variations on the theme shown below with no joy:
try {
// do something that may throw an exception
}
catch( exception e ) {
printf( "Exception: %s\n", e.what );
}
The compiler complains that identifier 'exception' not found.
I tried various things in place of 'exception' (e.g., 'IOException', 'bad_alloc') with no luck.
I tried different exception-related options in the project configuration, also with no luck.
I was able to make it compile using
catch(...) {
but that gives me no info about the exception.
Questions:
Are exceptions even allowed in non-.NET apps?
Am I missing some include file?
Some other (dumb) thing I did or didn't do?
Upvotes: 0
Views: 1274
Reputation: 1
Are exceptions even allowed in non-.NET apps?
Yes, they are.
Am I missing some include file?
It looks like you're missing the necessary #include <exception>
and/or the namespace scoping with std::
.
Some other (dumb) thing I did or didn't do?
The better way to catch exceptions is by const reference BTW:
#include <exception>
int main() {
try {
// do something that may throw an exception
}
catch(const std::exception& e ) {
printf( "Exception: %s\n", e.what );
}
}
Upvotes: 2