-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_exceptions.hpp
170 lines (152 loc) · 4.69 KB
/
utils_exceptions.hpp
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#ifndef UTILS_EXCEPTIONS_HPP
#define UTILS_EXCEPTIONS_HPP
#include <stdexcept>
#include <string>
namespace utils::exceptions {
// logic_error domain_error invalid_argument
// length_error out_of_range runtime_error
// range_error overflow_error underflow_error
/**
* \brief
* Generic exception with message.
* \param msg
* The message to be shown when encountering this exception.
*/
class Exception : public std::exception {
private:
std::string _msg;
public:
explicit Exception(const std::string& name, const std::string& msg)
: _msg(name + ": " + msg) {}
explicit Exception(const std::string& msg)
: Exception("Exception", msg) {}
virtual ~Exception() {}
virtual const char *what() const noexcept {
return this->getMessage().c_str();
}
virtual inline const std::string& getMessage() const noexcept {
return this->_msg;
}
};
/**
* \brief
* Out of bounds exception with message and index.
* \param idx
* The index that caused the out of bounds.
*/
class OutOfBoundsException : public Exception {
public:
OutOfBoundsException(const int& idx)
: Exception("OutOfBoundsException",
"Index " + std::to_string(idx) + " was out of bounds.")
{}
};
/**
* \brief
* Nullpointer exception with message.
* \param msg
* The null reference.
*/
class NullPointerException : public Exception {
public:
NullPointerException(const std::string& msg)
: Exception("NullPointerException",
"Reference " + msg + " not set to an object.")
{}
};
/**
* \brief
* Divide by zero exception with message.
* \param msg
* The index that caused the out of bounds.
*/
class DivideByZeroException : public Exception {
public:
DivideByZeroException(const std::string& msg)
: Exception("DivideByZeroException",
"Tried to divide by zero in " + msg)
{}
};
/**
* \brief
* Casting exception with message.
* \param src
* The object to be casted.
* \param dest
* The object to cast to.
*/
class CastingException : public Exception {
public:
CastingException(const std::string& src, const std::string& dest)
: Exception("CastingException",
"Cannot cast \"" + src + "\" to object of type \""
+ dest + "\"!")
{}
};
/**
* \brief
* File read exception.
* \param name
* The name of the file to be read.
*/
class FileReadException : public Exception {
public:
FileReadException(const std::string& name)
: Exception("FileReadException",
"Cannot read from file: " + name)
{}
};
/**
* \brief
* File write exception.
* \param name
* The name of the file to be written.
*/
class FileWriteException : public Exception {
public:
FileWriteException(const std::string& name)
: Exception("FileWriteException",
"Cannot write to file: " + name)
{}
};
/**
* \brief
* Unexpected file extension exception.
* \param ext
* The extension of a file that was not expected.
*/
class UnexpectedExtension : public Exception {
public:
UnexpectedExtension(const std::string& ext)
: Exception("UnexpectedExtension", ext)
{}
};
/**
* \brief
* Conversion exception.
* \param msg
* The task throwing the exception.
*/
class ConversionException : public Exception {
public:
ConversionException(const std::string& msg)
: Exception("ConversionException", msg)
{}
};
/**
* \brief
* KeyDoesNotExist exception.
* \param container
* The container in which the key was not found.
* \param key
* The key that was not found.
*/
class KeyDoesNotExistException : public Exception {
public:
KeyDoesNotExistException(const std::string& container, const std::string& key)
: Exception("KeyDoesNotExistException",
"The specified key '" + key + "' does not exist in " + container)
{}
};
}
#endif // UTILS_EXCEPTIONS_HPP