Reputation: 53
In java the compiler complains about uncatched exceptions.
I use exceptions in C++ and I miss that feature.
Is there a tool out there capable of doing it? maybe a compiler option (but I doubt it)
Upvotes: 1
Views: 720
Reputation: 4952
a static analyzer can run over your code and warn you if a function might throw an unhandled exception
for example good old pc-lint
or coverity
Upvotes: 3
Reputation: 84151
Java has checked exceptions, which is different from how C++ goes about it. One way of catching all exceptions is ...
syntax, as bellow:
try
{
// code here can throw
}
catch ( const std::exception& stde )
{
// handle expected exception
}
catch ( ... )
{
// handle unexpected exceptions
}
There's also a runtime mechanism to react to unexpected exceptions via set_unexpected()
, though its usefulness is debatable.
The preferred approach is to attempt writing exception-safe code.
Upvotes: 1
Reputation: 170469
You just have to catch all exceptions at the top level. Typically this will be enough:
try {
//do stuff
} catch( std::exception& e ) {
// log e.what() here
} catch( YourCustomExceptionHierarchyRoot& e) {
// Perhaps you have smth like MFC::CException in your library
// log e.MethodToShowErrorText() here
} catch( ... ) {
// log "unknown exception" here
}
you will need to do this at the top level of your program (smth like main()).
Also if you implement COM methods you'll have to do the same for each COM-exposed piece of code - throwing exceptions through the COM boundary is not allowed.
Upvotes: 1
Reputation:
There really isn't any way of doing that in C++. But it's easy enough to provide default exception handling at the top level of your program which will catch anything that got missed in the lower levels. Of course, you really don't want to catch most exceptions at this level, but you can at least provide reasonable diagnostic messages.
Upvotes: 3