]> git.tdb.fi Git - libs/core.git/blob - source/core/except.h
Bugfixes
[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         ~KeyError() throw() { }
52 private:
53         std::string key;
54 };
55
56 /**
57 Thrown when the current object state doesn't allow the requested action.
58 */
59 class InvalidState: public Exception
60 {
61 public:
62         InvalidState(const std::string &w_): Exception(w_) { }
63 };
64
65 /**
66 Thrown when the application is invoked with wrong parameters.
67 */
68 class UsageError: public Exception
69 {
70 public:
71         UsageError(const std::string &r, bool b=true): Exception(r), brief(b) { }
72         bool get_brief() const { return brief; }
73 private:
74         bool brief;
75 };
76
77 /**
78 Thrown when a system call fails.
79 */
80 class SystemError: public Exception
81 {
82 public:
83         SystemError(const std::string &, int);
84         int get_error_code() const { return err; }
85 private:
86         int err;
87
88         static std::string build_what(const std::string &, int);
89 };
90
91 } // namespace Msp
92
93 #endif