Here is the exception tester that I wrote based on the answers of @JPlatte and @gongzhitaao. For my test environment, I have two global variables num_test and num_test_success that track how many tests are running and how many of them are successful in a series of tests, but this can be changed to suit your needs.
int num_test; int num_test_success; #define UT_ASSERT_THROW(expression, ExceptionType) { \ \ try { \ expression; \ printf("test %d: *** FAILURE *** %s:%d:%s,%s\n", \ num_test, __FILE__, __LINE__, \ #expression, #ExceptionType); \ } catch (const ExceptionType &) { \ printf("test %d: success\n", num_test); \ ++num_test_success; \ } catch (...) { \ printf("test %d: *** FAILURE *** %s:%d:%s,%s\n", \ num_test, __FILE__, __LINE__, \ #expression, #ExceptionType); \ } \ \ ++num_test; \ \ }
The __FILE__ and __LINE__ expand to the current file and line number (see https://gcc.gnu.org/onlinedocs/cpp/Standard-Predefined-Macros.html ). The pound signs inside the macro indicate to the preprocessor the place of the argument as a string in the macro extension (see http://bruceblinn.com/linuxinfo/Stringification.html ).
dpritch
source share