Chcę utworzyć kilka typów wyjątków, które pochodzą od std :: runtime_error i chcę, aby miały typ stringstream. Mam w związku z tym stworzyliśmy klasę wyjątku, który komponuje std::stringstream
i wynikającemu z std::runtime_error
:Dlaczego nie mogę złapać tego wyjątku?
template<typename T>
class error_stream : public std::runtime_error
{
public:
error_stream() :
std::runtime_error(""), ss(std::make_shared<std::basic_stringstream<T>>())
{
}
~error_stream() throw()
{
}
template <typename T>
error_stream & operator << (const T & t)
{
*ss << t;
return *this;
}
virtual const char * what() const throw()
{
get_str(s_, ss->str());
return s_.c_str();
}
protected:
template <typename T>
void get_str(std::basic_string<char> & s_, const std::basic_string<T> & str) const
{
s_ = str;
}
template<>
void get_str(std::basic_string<char> & s_, const std::basic_string<wchar_t> & str) const
{
std::basic_string<char> ret(str.begin(), str.end());
s_ = ret;
}
protected:
std::shared_ptr<std::basic_stringstream<T>> ss;
mutable std::basic_string<char> s_;
};
I stworzyliśmy bardziej konkretny typ wyjątku, co z kolei wynika z tego error_stream
wyjątek:
template<typename T>
class w32file_exception : public w32utils::error_stream<T>
{
public:
w32file_exception() : error_stream<T>() {}
};
Jednak spotkałem się z czymś, czego tutaj nie rozumiem, ponieważ kiedy rzucam w32file_exception
, faktycznie mogę go złapać tylko jako rodzica error_stream
. Czy ktoś może zobaczyć, co robię źle?
try
{
throw w32file_exception<char>() << "test";
}
catch (w32file_exception<char> & e)
{
ASSERT_PASSED;
}
catch (error_stream<char> & e)
{
std::cout << e.what() << std::endl; // Why do I end up here?
}
Nie znam twojego przypadku użycia 'operatora <<', więc nie wiem, czy to pomaga - ale chciałem tylko wspomnieć o [Boost.Exception] (http://www.boost.org/ doc/libs/1_49_0/libs/exception/doc/boost-exception.html), a w szczególności [Transportowanie arbitralnych danych do strony catch] (http://www.boost.org/doc/libs/1_49_0/libs) /exception/doc/tutorial_transporting_data.html) –