Hi,
For a long time I’ve wondered if there is a simplier way to do this, but I’ve never found it. If I have the following code:
Now obviously the above code doesn’t work, because I can’t do “Invalid argment ‘” + arg. The alternative would be to do:Code:void SomeClass::SomeMethod(const char *arg) { If (someTestOfTheArgFailed) throw std::invalid_argument (“Invalid argument ‘” + arg + “’”); }
This works but has needlessly added extra lines.Code:If (someTestOfTheArgFailed) { std::string s; s += " Invalid argument '"; s += arg; s += "'"; throw std::invalid_argument (s); }
It would be cool if I could use some kind of printf syntax, or use operator<<.
Is there some clever trick I can use to do this all on one line still? Perhaps a function or macro I could develop? This is just so I can make my error handling code neater. I don't want to have 4-5 lines for each error message.
Thanks
Andrew




Reply With Quote
