मैं std :: runtime_error से प्राप्त कई अपवाद प्रकार बनाना चाहता हूं और मैं चाहता हूं कि उन्हें स्ट्रिंगस्ट्रीम प्रकार की कार्यक्षमता हो। इसलिए मैं एक अपवाद वर्ग है जो एक std::stringstream
तैयार करता है और जो std::runtime_error
से निकला बना लिया है:मैं इस अपवाद को क्यों नहीं पकड़ सकता?
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_;
};
और मैं एक अधिक विशिष्ट अपवाद प्रकार है जो बदले में इस error_stream
अपवाद से निकला बना लिया है:
template<typename T>
class w32file_exception : public w32utils::error_stream<T>
{
public:
w32file_exception() : error_stream<T>() {}
};
हालांकि, मुझे कुछ ऐसा सामना करना पड़ा है जो मुझे समझ में नहीं आता है, क्योंकि जब मैं w32file_exception
फेंकता हूं तो मैं वास्तव में इसे केवल error_stream
के रूप में पकड़ सकता हूं। क्या कोई देख सकता है कि मैं क्या गलत कर रहा हूं?
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?
}
मुझे 'ऑपरेटर <<' का उपयोग करने का मामला नहीं पता है, इसलिए मुझे नहीं पता कि इससे मदद मिलती है - लेकिन मैं बस [बूस्ट.एक्सप्शन] का उल्लेख करना चाहता था (http://www.boost.org/ डॉक्टर/libs/1_49_0/libs/अपवाद/डॉक्टर/बूस्ट-अपवाद.html) और विशेष रूप से [कैच साइट पर मनमाने ढंग से डेटा का परिवहन] (http://www.boost.org/doc/libs/1_49_0/libs /exception/doc/tutorial_transporting_data.html) –