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