It is not available out of the box. But if you are willing to add an extra exception class to your hierarchy it can be done like this (only tested on MSVC):
#include <iostream>
#include <exception>
#include <string>
class file_line_exception : public std::exception
{
public:
template<size_t N>
file_line_exception(const char(&file)[N], size_t line, const std::string& what) :
std::exception{ what.c_str() },
m_file{ file },
m_line{ std::to_string(line) }
{
}
const std::string& file() const noexcept
{
return m_file;
}
const std::string& line() const noexcept
{
return m_line;
}
private:
std::string m_file;
std::string m_line;
};
int main()
{
try
{
throw file_line_exception(__FILE__, __LINE__, "some kind of descrition");
}
catch (const file_line_exception& e)
{
std::cout << "exception from : " << e.file() << "(" << e.line() << ") : " << e.what() << std::endl;
}
}