posted 9 years ago
The standard defines two macros "__FILE__" and "__LINE__".
With a bit of macro-magic, these can be easily inlined into a throw statement, although you would have a bit of trouble enforcing its use. However, you should ask yourself the question: Should I be doing this in the beautiful [b]modern C++[/b] code that you've written?
[code=C++]
#include <string>
#include <iostream>
struct custom_exception
{
const char* what;
const char* file;
int line;
custom_exception(const char* what, const char* file, int line) :
what(what), file(file), line(line)
{
}
};
#define THROW(x) throw(custom_exception(x, __FILE__, __LINE__));
int main()
{
try
{
THROW("test");
}
catch(const custom_exception& ce)
{
std::cout << "Exception @ " << ce.file << ":" << ce.line << ":" << ce.what << std::endl;
}
}
[/code]