]> git.tdb.fi Git - libs/core.git/blob - source/core/except.h
Store the failed key in KeyError
[libs/core.git] / source / core / except.h
1 /* $Id$
2
3 This file is part of libmspcore
4 Copyright © 2006-2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7 #ifndef MSP_CORE_ERROR_H_
8 #define MSP_CORE_ERROR_H_
9
10 #include <exception>
11 #include <string>
12 #include "../debug/backtrace.h"
13
14 namespace Msp {
15
16 /**
17 Base class for all Msp exceptions.
18 */
19 class Exception: public std::exception
20 {
21 public:
22         Exception(const std::string &);
23         ~Exception() throw() { }
24
25         const char *what() const throw() { return w.c_str(); }
26         const Debug::Backtrace &get_backtrace() const throw() { return bt; }
27 private:
28         std::string w;
29         Debug::Backtrace bt;
30
31 };
32
33 /**
34 Thrown when a function parameter has an invalid value.
35 */
36 class InvalidParameterValue: public Exception
37 {
38 public:
39         InvalidParameterValue(const std::string &w_): Exception(w_) { }
40 };
41
42 /**
43 Thrown when a lookup from a map fails.
44 */
45 class KeyError: public Exception
46 {
47 public:
48         KeyError(const std::string &w_): Exception(w_) { }
49         KeyError(const std::string &w_, const std::string &k) { }
50         const std::string &get_key() const { return key; }
51 private:
52         std::string 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 public:
70         UsageError(const std::string &r, bool b=true): Exception(r), brief(b) { }
71         bool get_brief() const { return brief; }
72 private:
73         bool brief;
74 };
75
76 /**
77 Thrown when a system call fails.
78 */
79 class SystemError: public Exception
80 {
81 public:
82         SystemError(const std::string &, int);
83         int get_error_code() const { return err; }
84 private:
85         int err;
86
87         static std::string build_what(const std::string &, int);
88 };
89
90 } // namespace Msp
91
92 #endif