I’m trying to write two simple macros for begin and end of functions in order to manage and log all the Exceptions in my program. In other words, I wanna have a try-catch block in all of my functions.
in simple case consider the following macros.
#define __BEGIN_FUNCTION__ try {
#define __END_FUNCTION__ } catch(std::exception e) \
{ log << time << ':' << e << endl; }
but if I put these two macros in beginning and end of a function, the compiler will give the “Not all control paths return a value” error.
any solution?
That’s a horrible way to handle exceptions. If an exception is thrown, it is because an error occurred. You then have to either handle the error, solving the problem that occurred, or allow it to terminate the program.
Ignoring the exception makes no sense.
At the very least, you should rethrow the exception once it’s been logged. Change the
END_FUNCTIONmacro to include athrow;. That should also solve the compiler error you’re getting.A final note: your macro names are very badly chosen.
Names that either:
are reserved for use by the implementation (the compiler and the standard library.
You risk conflicts by using names like those anywhere in your program.
You should also note that catching exceptions should be done by reference (
catch (const std::exception& e), and that it won’t catch exceptions not derived fromstd::exception. If you want to catch everything that is thrown, usecatch(...).