У меня есть класс, который имеет несколько методов, таких как следующим (и более):
template<class T>
Logpp& operator<<(T const& obj)
{
*p_Stream << obj ;
return *this ;
}
Logpp& operator<<(char const* zString)
{
*p_Stream << zString ;
return *this ;
}
Logpp& operator<<(std::ostream& (*manip)(std::ostream&))
{
*p_Stream << manip;
return *this ;
}
Я хочу заключить тело функции в блок try catch вида:
Logpp& operator<<(std::ostream& (*manip)(std::ostream&))
{
try
{
*p_Stream << manip;
return *this;
}
catch(ios_base::failure& e)
{
//MyException has a stringstream inside and can use operator<<
throw MyException("IO failure writing to log file : ") << e.what() << endl;
}
}
В1: Желательно ли использовать подобные исключения? (В каждой функции). Я не знаком с использованием исключений, поэтому я не уверен.
Q2: Если ответ на вопрос Q1 положительный, могу ли я сделать что-то подобное для устранения избыточности?
Logpp& operator<<(std::ostream& (*manip)(std::ostream&))
{
Catch<ios_base::failure> tc();
try
{
*p_Stream << manip;
return *this;
}
//destructor of tc will be written to catch the template exception type and rethrow as a MyException.
}