]> git.tdb.fi Git - libs/core.git/blob - source/core/except.h
ed295f11cef6c50d7fb1c407a7c89852e2d8b2b6
[libs/core.git] / source / core / except.h
1 #ifndef MSP_CORE_ERROR_H_
2 #define MSP_CORE_ERROR_H_
3
4 #include <exception>
5 #include <string>
6 #include <msp/debug/backtrace.h>
7
8 namespace Msp {
9
10 /**
11 Base class for all Msp exceptions.
12 */
13 class Exception: public std::exception
14 {
15 private:
16         std::string wot;
17         std::string wer;
18         Debug::Backtrace bt;
19
20 public:
21         Exception(const std::string &);
22         ~Exception() throw() { }
23
24         const char *what() const throw() { return wot.c_str(); }
25         Exception &at(const std::string &) throw();
26         const char *where() const throw() { return wer.c_str(); }
27         const Debug::Backtrace &get_backtrace() const throw() { return bt; }
28 };
29
30 /**
31 Thrown when a function parameter has an invalid value.
32 */
33 class InvalidParameterValue: public Exception
34 {
35 public:
36         InvalidParameterValue(const std::string &w_): Exception(w_) { }
37 };
38
39 /**
40 Thrown when a lookup from a map fails.
41 */
42 class KeyError: public Exception
43 {
44 private:
45         std::string key;
46
47 public:
48         KeyError(const std::string &w_): Exception(w_) { }
49         KeyError(const std::string &w_, const std::string &k);
50         ~KeyError() throw() { }
51
52         const std::string &get_key() const { return key; }
53 };
54
55 /**
56 Thrown when the current object state doesn't allow the requested action.
57 */
58 class InvalidState: public Exception
59 {
60 public:
61         InvalidState(const std::string &w_): Exception(w_) { }
62 };
63
64 /**
65 Thrown when the application is invoked with wrong parameters.
66 */
67 class UsageError: public Exception
68 {
69 private:
70         bool brief;
71
72 public:
73         UsageError(const std::string &r, bool b = true): Exception(r), brief(b) { }
74         bool get_brief() const { return brief; }
75 };
76
77 /**
78 Thrown when a system call fails.
79 */
80 class SystemError: public Exception
81 {
82 private:
83         int err;
84
85 public:
86         SystemError(const std::string &, int);
87         int get_error_code() const { return err; }
88
89 private:
90         static std::string build_what(const std::string &, int);
91 };
92
93 /**
94 Thrown when "impossible" things happen.
95 */
96 class LogicError: public Exception
97 {
98 public:
99         LogicError(const std::string &w_): Exception(w_) { }
100 };
101
102 template<typename E>
103 void throw_at(E e, const std::string &a)
104 { e.at(a); throw e; }
105
106 } // namespace Msp
107
108 #endif