|
Boost Users : |
From: bert hubert (bert.hubert_at_[hidden])
Date: 2007-01-24 07:35:58
Hi people,
I find that I rely on lexical_cast<> a lot, it is a great thing. However,
when it throws an exception, and I catch it, I lose information on which of
the many lexical_casts had a problem. This is not a lexical_cast specific
problem of course, it would be great to have a more general way to add line
numbers to exceptions!
This led me to code up this small gcc specific hack, but I wonder if there
is a better way:
#define wrapped_atox(x, y) ({ \
x tmp; \
try { tmp=boost::lexical_cast<x>(y); } \
catch(exception &e) { \
throw std::runtime_error(std::string("On line " + \
boost::lexical_cast<std::string>(__LINE__)+" of " +\
std::string(__PRETTY_FUNCTION__)+" in " +std::string(__FILE__) +\
", while converting '"+string(y)+"': ") + e.what()); \
} \
tmp; \
})
Which can be used as:
cout << wrapped_atox(int, argv[1]) << endl;
The code above works, but is gcc specific, and well, it doesn't look right.
Perhaps someone has a better idea? Otherwise, people may benefit from the
snippet above, feel free to copy it!
Bert
-- http://www.PowerDNS.com Open source, database driven DNS Software http://netherlabs.nl Open and Closed source services
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net