views:

176

answers:

3

I was wondering if there is some compiler parameter, preferably in gcc (g++) which treats the lack of try/catch blocks as errors. This is the standard behavior in java and I was alway fond of it.

+7  A: 

Since checked exceptions in Java rely on the throw signature, you can read why you will not want to use throw function signatures in C++ in this question on SO.

OregonGhost
A: 

One thing you can do in C++ with exceptions is use exception specifications on your functions. That doesn't actively prevent non-listed exceptions from being thrown from that function, but it makes them errors (and maps them all to the predefined unexpected().

So int f() throw (); is C++ for "treat any exception being raised from function f as an error".

T.E.D.
Probably worth noting that it's "treat as _runtime_ error". A C++ compiler is still _required_ to compile `void foo() throw() { throw 42; }`.
Pavel Minaev
+1  A: 

Well, using exception specifications is generally a bad idea: http://cplusplus.co.il/2009/10/06/exception-specifications/

And if not using these, the compiler basically has no way of knowing which exception may be thrown, so there's no way to do that.

What you could do, is provide your own implementation of std::terminate (by invoking std::set_terminate()) and handle uncaught exceptions there.

rmn