728x90
반응형
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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
반응형
'C C++' 카테고리의 다른 글
QtConcurrent : MapReduce 모델에 대한 Qt의 대안 (0) | 2011.01.08 |
---|---|
const object 상수형 객체 (0) | 2010.09.09 |
[CodeBlocks & wxPack] 손쉽게 Windows에서 wxWidgets(wxWindows) 개발 환경 구성하기 (3) | 2009.10.30 |
Google ctemplate (0) | 2009.07.08 |
C++ 함수 가림 현상(name hiding)과 using 키워드 활용하기 (0) | 2009.03.08 |