]> git.tdb.fi Git - libs/core.git/blob - source/core/except.h
Add a "where" property to Exception class
[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 public:
23         Exception(const std::string &);
24         ~Exception() throw() { }
25
26         const char *what() const throw() { return wot.c_str(); }
27         Exception &at(const std::string &) throw();
28         const char *where() const throw() { return wer.c_str(); }
29         const Debug::Backtrace &get_backtrace() const throw() { return bt; }
30 private:
31         std::string wot;
32         std::string wer;
33         Debug::Backtrace bt;
34
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 public:
52         KeyError(const std::string &w_): Exception(w_) { }
53         KeyError(const std::string &w_, const std::string &k);
54         const std::string &get_key() const { return key; }
55         ~KeyError() throw() { }
56 private:
57         std::string key;
58 };
59
60 /**
61 Thrown when the current object state doesn't allow the requested action.
62 */
63 class InvalidState: public Exception
64 {
65 public:
66         InvalidState(const std::string &w_): Exception(w_) { }
67 };
68
69 /**
70 Thrown when the application is invoked with wrong parameters.
71 */
72 class UsageError: public Exception
73 {
74 public:
75         UsageError(const std::string &r, bool b=true): Exception(r), brief(b) { }
76         bool get_brief() const { return brief; }
77 private:
78         bool brief;
79 };
80
81 /**
82 Thrown when a system call fails.
83 */
84 class SystemError: public Exception
85 {
86 public:
87         SystemError(const std::string &, int);
88         int get_error_code() const { return err; }
89 private:
90         int err;
91
92         static std::string build_what(const std::string &, int);
93 };
94
95 template<typename E>
96 void throw_at(E e, const std::string &a)
97 { e.at(a); throw e; }
98
99 } // namespace Msp
100
101 #endif