code snippet to add line number to lexical_cast exceptions
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_caststd::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
participants (1)
-
bert hubert