views:

130

answers:

4

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)

+3  A: 

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.

anon
are you talking about the terminate handler?
laura
no, I'm talking about writing a try block around all your code in main
anon
Or you could use the 'SetUnhandledExceptionFilter' for MS platforms.http://stackoverflow.com/questions/1796225/get-stack-trace-from-uncaught-exception/1796845#1796845
Danny Parker
+3  A: 

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

Alon
I was thinking about something you can integrate in the build process.Those 2 looks promising I'll take a look!but do you know any open source / free equivalent?
f4
+1  A: 

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.

sharptooth
+1  A: 
Nikolai N Fetissov
Writing exception safe code doesn't stop exceptions being thrown, and doesn't address the OP's question.
anon
Thanks. I know. I'm talking about how the concept is different in Java and C++.
Nikolai N Fetissov
+1 for signalling the debatable usefulness. Also, many programmers in Java corrupt the system by using `try / catch(...)` and just plainly ignore the exceptions...
Matthieu M.
@Neil: I think it is fair to signal to the OP the problem of using this system, if finally he recognizes the problems this causes he'll save time NOT trying to do it.
Matthieu M.