Jpp 21.0.0-rc.1
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#include <functional>
9
10
11/**
12 * General exception
13 */
14class Exception : public std::exception {
15public:
16 /**
17 * Constructor.
18 *
19 * \param error error message
20 */
21 Exception(const std::string& error) :
22 std::exception(),
23 buffer(error)
24 {}
25
26
27 /**
28 * Destructor.
29 */
30 ~Exception() throw()
31 {}
32
33
34 /**
35 * Get error message.
36 *
37 * \return error message
38 */
39 virtual const char* what() const throw()
40 {
41 return buffer.c_str();
42 }
43
44
45 /**
46 * Print error message of JException.
47 *
48 * \param out output stream
49 * \param exception exception
50 */
51 friend inline std::ostream& operator<<(std::ostream& out, const Exception& exception)
52 {
53 return out << exception.what();
54 }
55
56private:
57 const std::string buffer;
58};
59
60
61/**
62 * Make exception.
63 *
64 * \param JException_t exception
65 * \param A message
66 */
67#ifndef MAKE_EXCEPTION
68#define MAKE_EXCEPTION(JException_t, A) std::invoke( [&]() { std::ostringstream out; out << __FILE__ << ':' << __LINE__ << std::endl << A; return JException_t(out.str()); } )
69#endif
70
71/**
72 * Marco for throwing exception with std::ostream compatible message.
73 *
74 * \param Exception_t exception
75 * \param A message
76 */
77#ifndef THROW
78#define THROW(JException_t, A) do { throw MAKE_EXCEPTION(JException_t, A); } while(0)
79#endif
80
81#endif
General exception.
Definition Exception.hh:14
~Exception()
Destructor.
Definition Exception.hh:30
virtual const char * what() const
Get error message.
Definition Exception.hh:39
const std::string buffer
Definition Exception.hh:57
Exception(const std::string &error)
Constructor.
Definition Exception.hh:21
friend std::ostream & operator<<(std::ostream &out, const Exception &exception)
Print error message of JException.
Definition Exception.hh:51