How to get the exception line from the call point.

The below code throws error in the line number 32 where the function is defined.

But How to find the line where the function is called.
That is I want to throw the error at the line number 43 (as here the function is called).

(Using macros works well. But I dont want to use macros.)

The code is:

#include <iostream>
#include <string>
#include <sstream>
using namespace std;
class myEx {
std:: string _file, _message;
int _line;
public:
myEx(std::string file, int line);
std::string toString();
};
myEx::myEx(string file, int line) {
_file = file;
_line = line;
_message = "Unable to copy. ";
toString();
}
string myEx::toString()
{
ostringstream os;
os << _message <<"Error occurred at " << _file << ":" << _line;
return os.str();
}
template <typename T, int size_x>
static void toCopy(T(&x)[size_x], const T(*y)) {
if(strlen(y) >= size_x) {
throw myEx(__FILE__, __LINE__);
}
strncpy(x, y, size_x);
}

int main()
{
char first[4];
string second = "WELCOME";
try {
toCopy(first, second.c_str());
cout << "Copied Successfully! the " <<first<<endl;
}
catch(myEx &e)
{
cout << e.toString() << endl;
}
return 0;
}
There isn't really a way without macros.
Topic archived. No new replies allowed.