Wrapping a boolean return value with an exception

I am currently working with a long block of code that uses && (logical and) to a long list of functions. The idea is to run all of the functions and short circuit if any of them fail. The problem is that there is no consistant mechanism for error reportin. If any of the functions fail, I have no way of knowing which one. This is my planned approach.

#include <iostream>
#include <stdexcept>
using namespace std;

bool invert(bool val){
return !val;
}

#define attempt( X ){ if (!X){ throw runtime_error(#X);}}

int main(){
cout << “OK” << endl;
try{
attempt(invert(true));
}catch(runtime_error& re){
cout << “runtime_error exception:” << re.what() <<endl;
}
return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.