I'm currently using caffe but I'm facing a problem. Sometime the library call LOG(FATAL)
but I would like to raise an exception and catch it.
I have tried to do my own class defined as below:
#include <stdexcept>
#include <iostream>
#include <sstream>
class FatalException
{
private:
std::stringstream _ss;
public:
template<typename T>
std::ostream& operator<<(const T& obj){
_ss << obj;
return _ss;
}
~FatalException(){
throw std::runtime_error(_ss.str().c_str());
}
};
The problem is that when I'm doing some testing such as
int main() {
try {
FatalException() << "test";
}
catch(...) {
std::cout << "here" << std::endl;
}
}
the exception is thrown after the try
scope
Do you have any hints? Should I overload a stream class and throw an exception when the stream is flushed?