At certain points of my code I want to throw an exception and give it some information about the code where I've thrown it. Something like
void Foo() {
if(condition)
throw std::logic_error(ERROR_MSG);
}
// somewhere later in code
try {
Foo()
} catch (std::exception & e) {
e.what();
}
Now the question is – how do i construct this error message? I would like to give information about the file, the line of code, the function name and a bit of an own information. Something (I suspect) should be pretty standard usage – but after an extensive search I still didn't find something useful about this (as I thought) easy subject.
I just would like to write something like this
throw std::logic_arrow("Whoops! Error in (" +
__func__ + ") " + __FILE__ + ":" + __LINE__);
But this doesn't seem to work for various reasons. Here is what I achieved so far with my search:
- There is already a similar question here, but this is about if one should use these macros, not how
- This question deals with concatenating
__FILE__
and__LINE__
in a macro, so I was a bit further to my answer - The
__func__
macro seems to make difficulties, since "These identifiers are variables, not preprocessor macros, and may not be used to initialize char arrays or be concatenated with string literals" following to the gcc homepage. Well, it is now a variable I can use for instance in theprintf
function like here – but I didn't manage to master the transfer to my own problem - One guy was worried about a cleaner solution than Macros, using inline functions, so I could even improve a solution
- C++14 techniques where suggested in another thread, using
operator ""s