-
Notifications
You must be signed in to change notification settings - Fork 3
/
exceptions.h
113 lines (93 loc) · 3.5 KB
/
exceptions.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. See the enclosed file LICENSE for a copy or if
* that was not distributed with this file, You can obtain one at
* http://mozilla.org/MPL/2.0/.
*
* Copyright 2017 Max H. Gerlach
*
* */
/*
* exceptions.h
*
* Created on: Dec 6, 2012
* Author: gerlach
*/
#ifndef EXCEPTIONS_H_
#define EXCEPTIONS_H_
#include <exception>
#include <string>
#include "tools.h"
class GeneralError : public std::exception {
std::string message;
public:
GeneralError(const std::string& msg, const char* file = 0, int line = -1)
{
if (file and line >= 0) {
message = file + (":" + numToString(line)) + " -> " + msg;
} else {
message = msg;
}
}
virtual ~GeneralError() throw () { }
virtual const char* what() const throw () {
return message.c_str();
}
};
#define throw_GeneralError(msg) throw GeneralError(msg, __FILE__, __LINE__)
class WrongObsIndex : public GeneralError {
public:
WrongObsIndex(int obsIndex, bool vector=false, const char* file = 0, int line = -1)
: GeneralError(((vector ? "Vector " : "") + std::string("Observable index ") + numToString(obsIndex) +
" not supported"), file, line)
{ }
};
#define throw_WrongObsIndex(obsIndex, vector) throw WrongObsIndex(obsIndex, vector, __FILE__, __LINE__)
class ParameterMissing : public GeneralError {
public:
ParameterMissing(const std::string& par, const char* file = 0, int line = -1)
: GeneralError("Parameter " + par + " not given.", file, line)
{ }
};
#define throw_ParameterMissing(par) throw ParameterMissing(par, __FILE__, __LINE__)
class ParameterWrong : public GeneralError {
public:
template <typename ValType>
ParameterWrong(const std::string& par, const ValType& val, const char* file = 0, int line = -1)
: GeneralError("Parameter " + par + " has incorrect value "
+ numToString(val), file, line)
{ }
ParameterWrong(const std::string& message, const char* file = 0, int line = -1)
: GeneralError(message, file, line)
{ }
};
#define throw_ParameterWrong_message(message) throw ParameterWrong(message, __FILE__, __LINE__)
#define throw_ParameterWrong(par, val) throw ParameterWrong(par, val, __FILE__, __LINE__)
class ConfigurationError : public GeneralError {
public:
ConfigurationError(const std::string& msg, const char* file = 0, int line = -1)
: GeneralError(msg, file, line)
{ }
};
#define throw_ConfigurationError(message) throw ConfigurationError(message, __FILE__, __LINE__)
class SerializationError : public GeneralError {
public:
SerializationError(const std::string& msg, const char* file = 0, int line = -1)
: GeneralError(msg, file, line)
{}
};
#define throw_SerializationError(message) throw SerializationError(message, __FILE__, __LINE__)
class ReadError : public GeneralError {
public:
ReadError(std::string filename, const char* file = 0, int line = -1) throw ()
: GeneralError("Can't read from file " + filename, file, line)
{ }
};
#define throw_ReadError(message) throw ReadError(message, __FILE__, __LINE__)
class KeyUndefined : public GeneralError {
public:
KeyUndefined(const std::string& key = "", const char* file = 0, int line = -1)
: GeneralError("key undefined: " + key, file, line)
{ }
};
#define throw_KeyUndefined(key) throw KeyUndefined(key, __FILE__, __LINE__)
#endif /* EXCEPTIONS_H_ */