-
Notifications
You must be signed in to change notification settings - Fork 0
/
Exceptions.hpp
153 lines (130 loc) · 4.32 KB
/
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
#ifndef EXCEPTIONS_HPP
#define EXCEPTIONS_HPP
#include <stdexcept>
#include <string>
using namespace std;
namespace 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 exception {
private:
string _msg;
public:
explicit Exception(const string &msg) : _msg(msg) {}
virtual ~Exception() /*_GLIBCXX_USE_NOEXCEPT*/ {}
virtual const string getMessage() const { /*_GLIBCXX_USE_NOEXCEPT*/
return "Exception: " + _msg;
}
virtual const string getMsg() const { /*_GLIBCXX_USE_NOEXCEPT*/
return _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("Index " + to_string(idx) + " was out of bounds.") {}
const string getMessage() const { /*_GLIBCXX_USE_NOEXCEPT*/
return "OutOfBoundsException: " + Exception::getMsg();
}
};
/**
* \brief
* Nullpointer exception with message.
* \param msg
* The null reference.
*/
class NullPointerException : public Exception {
public:
NullPointerException(const string &msg)
: Exception("Reference " + msg + " not set to an object.") {}
const string getMessage() const { /*_GLIBCXX_USE_NOEXCEPT*/
return "NullPointerException: " + Exception::getMsg();
}
};
/**
* \brief
* Divide by zero exception with message.
* \param msg
* The index that caused the out of bounds.
*/
class DivideByZeroException : public Exception {
public:
DivideByZeroException(const string &msg)
: Exception("Tried to devide by zero in " + msg) {}
const string getMessage() const { /*_GLIBCXX_USE_NOEXCEPT*/
return "DivideByZeroException: " + Exception::getMsg();
}
};
/**
* \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 string &src, const string &dest)
: Exception("Cannot cast \"" + src + "\" to object of type \"" + dest + "\"!") {}
const string getMessage() const { /*_GLIBCXX_USE_NOEXCEPT*/
return "CastingException: " + Exception::getMsg();
}
};
/**
* \brief
* File read exception.
* \param name
* The name of the file to be read.
*/
class FileReadException : public Exception {
public:
FileReadException(const string &name)
: Exception("Cannot read from file: " + name) {}
const string getMessage() const { /*_GLIBCXX_USE_NOEXCEPT*/
return "FileReadException: " + Exception::getMsg();
}
};
/**
* \brief
* File write exception.
* \param name
* The name of the file to be written.
*/
class FileWriteException : public Exception {
public:
FileWriteException(const string &name)
: Exception("Cannot write to file: " + name) {}
const string getMessage() const { /*_GLIBCXX_USE_NOEXCEPT*/
return "FileWriteException: " + Exception::getMsg();
}
};
/**
* \brief
* Unexpected file extension exception.
* \param ext
* The extension of a file that was not expected.
*/
class UnexpectedExtension : public Exception {
public:
UnexpectedExtension(string ext)
: Exception(ext) {}
const string getMessage() const { /*_GLIBCXX_USE_NOEXCEPT*/
return "UnexpectedExtension: " + Exception::getMsg();
}
};
}
#endif // EXCEPTIONS_HPP