Jpp 19.3.0-rc.3
the software that should make you happy
Loading...
Searching...
No Matches
Exception.hh
Go to the documentation of this file.
1#ifndef EXCEPTION_HH_INCLUDED
2#define EXCEPTION_HH_INCLUDED
3
4#include <exception>
5#include <string>
6#include <ostream>
7#include <sstream>
8
9
10/**
11 * General exception
12 */
13class Exception : public std::exception {
14public:
15 /**
16 * Constructor.
17 *
18 * \param error error message
19 */
20 Exception(const std::string& error) :
21 std::exception(),
22 buffer(error)
23 {}
24
25
26 /**
27 * Destructor.
28 */
29 ~Exception() throw()
30 {}
31
32
33 /**
34 * Get error message.
35 *
36 * \return error message
37 */
38 virtual const char* what() const throw()
39 {
40 return buffer.c_str();
41 }
42
43
44 /**
45 * Print error message of JException.
46 *
47 * \param out output stream
48 * \param exception exception
49 */
50 friend inline std::ostream& operator<<(std::ostream& out, const Exception& exception)
51 {
52 return out << exception.what();
53 }
54
55
56 /**
57 * Get output stream for conversion of exception.
58 *
59 * Note that the ostream is emptied before use.
60 *
61 * \return ostream
62 */
63 static inline std::ostream& getOstream()
64 {
65 static std::ostringstream buffer;
66
67 buffer.str("");
68
69 return buffer;
70 }
71
72private:
73 const std::string buffer;
74};
75
76
77/**
78 * Marco for throwing exception with std::ostream compatible message.
79 *
80 * \param Exception_t exception
81 * \param A message
82 */
83#ifndef THROW
84#define THROW(Exception_t, A) do { throw Exception_t(static_cast<std::ostringstream&>(Exception::getOstream() << __FILE__ << ':' << __LINE__ << std::endl << A).str()); } while(0)
85#endif
86
87#endif
General exception.
Definition Exception.hh:13
static std::ostream & getOstream()
Get output stream for conversion of exception.
Definition Exception.hh:63
~Exception()
Destructor.
Definition Exception.hh:29
virtual const char * what() const
Get error message.
Definition Exception.hh:38
const std::string buffer
Definition Exception.hh:73
Exception(const std::string &error)
Constructor.
Definition Exception.hh:20
friend std::ostream & operator<<(std::ostream &out, const Exception &exception)
Print error message of JException.
Definition Exception.hh:50