728x90
반응형
// code from Dr.Dobbs (http://www.drdobbs.com/)
// StreamingException.h
//
#ifndef STREAMING_EXCEPTION
#define STREAMING_EXCEPTION
#include <iostream>
#include <sstream>
#include <memory>
#include <stdexcept>
class StreamingException : public std::runtime_error
{
public:
StreamingException() :
std::runtime_error(""),
ss_(std::auto_ptr<std::stringstream>
(new std::stringstream()))
{
}
~StreamingException() throw()
{
}
template <typename T>
StreamingException & operator << (const T & t)
{
(*ss_) << t;
return *this;
}
virtual const char * what() const throw()
{
s_ = ss_->str();
return s_.c_str();
}
private:
mutable std::auto_ptr<std::stringstream> ss_;
mutable std::string s_;
};
#endif
// testException.cpp
#include "StreamingException.h"
int main (int argc, char * const argv[])
{
try
{
if (5 != 3)
throw StreamingException() << 5 << " is not equal to " << 3;
}
catch (const StreamingException & e)
{
cout << e.what() << endl;
}
return 0;
}
728x90
반응형

+ Recent posts