Skip to content

Commit 0d2c42d

Browse files
committed
Simplify exception classes
* Remove Exception class in favour of using std::runtime_error directly * Remove unused or unnecessary exception classes
1 parent c0fe4f7 commit 0d2c42d

24 files changed

Lines changed: 92 additions & 134 deletions

src/core/array.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,15 @@ template <class T> class Array {
5959
const_reference operator[](int index) const
6060
{
6161
if (index < m_offset || index > back_index()) {
62-
throw IndexException();
62+
throw std::out_of_range("Index out of range in Array");
6363
}
6464
return m_data.at(index - m_offset);
6565
}
6666

6767
reference operator[](int index)
6868
{
6969
if (index < m_offset || index > back_index()) {
70-
throw IndexException();
70+
throw std::out_of_range("Index out of range in Array");
7171
}
7272
return m_data[index - m_offset];
7373
}

src/core/function.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ class FunctionOnSimplices : public Function {
4545
void Project(Vector<double> &x, const Array<int> &lengths) const;
4646
};
4747

48-
class FunctionMinimizerError : public Exception {
48+
class FunctionMinimizerError : public std::runtime_error {
4949
public:
50+
FunctionMinimizerError() : std::runtime_error("Internal error in function minimization") {}
5051
~FunctionMinimizerError() noexcept override = default;
51-
const char *what() const noexcept override { return "Internal error in function minimization"; }
5252
};
5353

5454
// An abstract base class for function minimization

src/core/integer.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,10 @@ void scpy(const unsigned short *src, unsigned short *dest, int nb)
164164
}
165165

166166
// make sure an argument is valid
167-
168-
static inline void nonnil(const IntegerRep *rep)
167+
inline void nonnil(const IntegerRep *rep)
169168
{
170169
if (rep == nullptr) {
171-
throw Gambit::NullException();
170+
throw std::runtime_error("Null IntegerRep pointer");
172171
}
173172
}
174173

src/core/list.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,15 @@ template <class T> class List {
6161
const T &operator[](size_type p_index) const
6262
{
6363
if (p_index < 1 || p_index > m_list.size()) {
64-
throw IndexException();
64+
throw std::out_of_range("Index out of range in List");
6565
}
6666
return *std::next(m_list.cbegin(), p_index - 1);
6767
}
6868

6969
T &operator[](size_type p_index)
7070
{
7171
if (p_index < 1 || p_index > m_list.size()) {
72-
throw IndexException();
72+
throw std::out_of_range("Index out of range in List");
7373
}
7474
return *std::next(m_list.begin(), p_index - 1);
7575
}

src/core/matrix.imp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ template <class T> void Matrix<T>::MakeIdent()
399399
template <class T> void Matrix<T>::Pivot(int row, int col)
400400
{
401401
if (!this->CheckRow(row) || !this->CheckColumn(col)) {
402-
throw IndexException();
402+
throw std::out_of_range("Index out of range in Matrix::Pivot");
403403
}
404404
if (this->data[row][col] == (T)0) {
405405
throw ZeroDivideException();

src/core/recarray.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,14 @@ template <class T> class RectArray {
8585
T &operator()(int r, int c)
8686
{
8787
if (!Check(r, c)) {
88-
throw IndexException();
88+
throw std::out_of_range("Index out of range in RectArray");
8989
}
9090
return data[r][c];
9191
}
9292
const T &operator()(int r, int c) const
9393
{
9494
if (!Check(r, c)) {
95-
throw IndexException();
95+
throw std::out_of_range("Index out of range in RectArray");
9696
}
9797
return data[r][c];
9898
}
@@ -108,8 +108,8 @@ template <class T> class RectArray {
108108
//@{
109109
void SwitchRows(int i, int j)
110110
{
111-
if (!CheckRow(i) || !CheckRow(j)) {
112-
throw IndexException();
111+
if (!Check(i, j)) {
112+
throw std::out_of_range("Index out of range in RectArray");
113113
}
114114
std::swap(data[i], data[j]);
115115
}
@@ -205,7 +205,7 @@ template <class T> RectArray<T> &RectArray<T>::operator=(const RectArray<T> &a)
205205
template <class T> void RectArray<T>::RotateUp(int lo, int hi)
206206
{
207207
if (lo < minrow || hi < lo || maxrow < hi) {
208-
throw IndexException();
208+
throw std::out_of_range("Index out of range in RectArray");
209209
}
210210

211211
T *temp = data[lo];
@@ -218,7 +218,7 @@ template <class T> void RectArray<T>::RotateUp(int lo, int hi)
218218
template <class T> void RectArray<T>::RotateDown(int lo, int hi)
219219
{
220220
if (lo < minrow || hi < lo || maxrow < hi) {
221-
throw IndexException();
221+
throw std::out_of_range("Index out of range in RectArray");
222222
}
223223

224224
T *temp = data[hi];
@@ -235,7 +235,7 @@ template <class T> void RectArray<T>::RotateDown(int lo, int hi)
235235
template <class T> template <class Vector> void RectArray<T>::GetRow(int row, Vector &v) const
236236
{
237237
if (!CheckRow(row)) {
238-
throw IndexException();
238+
throw std::out_of_range("Index out of range in RectArray");
239239
}
240240
if (!CheckRow(v)) {
241241
throw DimensionException();
@@ -253,7 +253,7 @@ template <class T> template <class Vector> void RectArray<T>::GetRow(int row, Ve
253253
template <class T> template <class Vector> void RectArray<T>::GetColumn(int col, Vector &v) const
254254
{
255255
if (!CheckColumn(col)) {
256-
throw IndexException();
256+
throw std::out_of_range("Index out of range in RectArray");
257257
}
258258
if (!CheckColumn(v)) {
259259
throw DimensionException();
@@ -266,7 +266,7 @@ template <class T> template <class Vector> void RectArray<T>::GetColumn(int col,
266266
template <class T> template <class Vector> void RectArray<T>::SetColumn(int col, const Vector &v)
267267
{
268268
if (!CheckColumn(col)) {
269-
throw IndexException();
269+
throw std::out_of_range("Index out of range in RectArray");
270270
}
271271
if (!CheckColumn(v)) {
272272
throw DimensionException();

src/core/sqmatrix.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@
2727

2828
namespace Gambit {
2929

30-
class SingularMatrixException : public Exception {
30+
class SingularMatrixException final : public std::runtime_error {
3131
public:
32+
SingularMatrixException() : std::runtime_error("Attempted to invert a singular matrix") {}
3233
~SingularMatrixException() noexcept override = default;
33-
const char *what() const noexcept override { return "Attempted to invert a singular matrix"; }
3434
};
3535

3636
template <class T> class SquareMatrix : public Matrix<T> {

src/core/util.h

Lines changed: 9 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -67,70 +67,30 @@ template <class Key, class T> bool contains(const std::map<Key, T> &map, const K
6767
// Exception classes
6868
//========================================================================
6969

70-
/// A base class for all Gambit exceptions
71-
class Exception : public std::runtime_error {
72-
public:
73-
Exception() : std::runtime_error("") {}
74-
explicit Exception(const std::string &s) : std::runtime_error(s) {}
75-
~Exception() noexcept override = default;
76-
};
77-
78-
/// Exception thrown on out-of-range index
79-
class IndexException : public Exception {
80-
public:
81-
IndexException() : Exception("Index out of range") {}
82-
explicit IndexException(const std::string &s) : Exception(s) {}
83-
~IndexException() noexcept override = default;
84-
};
85-
86-
/// Exception thrown on invalid index ranges
87-
class RangeException : public Exception {
88-
public:
89-
RangeException() : Exception("Invalid index range") {}
90-
explicit RangeException(const std::string &s) : Exception(s) {}
91-
~RangeException() noexcept override = default;
92-
};
93-
9470
/// Exception thrown on dimension mismatches
95-
class DimensionException : public Exception {
71+
class DimensionException final : public std::runtime_error {
9672
public:
97-
DimensionException() : Exception("Mismatched dimensions") {}
98-
explicit DimensionException(const std::string &s) : Exception(s) {}
73+
DimensionException() : std::runtime_error("Mismatched dimensions") {}
74+
explicit DimensionException(const std::string &s) : std::runtime_error(s) {}
9975
~DimensionException() noexcept override = default;
10076
};
10177

10278
/// Exception thrown on invalid value
103-
class ValueException : public Exception {
79+
class ValueException final : public std::runtime_error {
10480
public:
105-
ValueException() : Exception("Invalid value") {}
106-
explicit ValueException(const std::string &s) : Exception(s) {}
81+
ValueException() : std::runtime_error("Invalid value") {}
82+
explicit ValueException(const std::string &s) : std::runtime_error(s) {}
10783
~ValueException() noexcept override = default;
10884
};
10985

110-
/// Exception thrown on a failed assertion
111-
class AssertionException : public Exception {
112-
public:
113-
AssertionException() : Exception("Failed assertion") {}
114-
explicit AssertionException(const std::string &s) : Exception(s) {}
115-
~AssertionException() noexcept override = default;
116-
};
117-
11886
/// Exception thrown on attempted division by zero
119-
class ZeroDivideException : public Exception {
87+
class ZeroDivideException final : public std::runtime_error {
12088
public:
121-
ZeroDivideException() : Exception("Attempted division by zero") {}
122-
explicit ZeroDivideException(const std::string &s) : Exception(s) {}
89+
ZeroDivideException() : std::runtime_error("Attempted division by zero") {}
90+
explicit ZeroDivideException(const std::string &s) : std::runtime_error(s) {}
12391
~ZeroDivideException() noexcept override = default;
12492
};
12593

126-
/// An exception thrown when attempting to dereference a null pointer
127-
class NullException : public Exception {
128-
public:
129-
NullException() : Exception("Dereferenced null pointer") {}
130-
explicit NullException(const std::string &s) : Exception(s) {}
131-
~NullException() noexcept override = default;
132-
};
133-
13494
} // end namespace Gambit
13595

13696
#endif // GAMBIT_CORE_UTIL_H

src/games/game.h

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,38 +71,35 @@ template <class T> class MixedBehaviorProfile;
7171
//=======================================================================
7272

7373
/// Exception thrown when an operation that is undefined is attempted
74-
class UndefinedException : public Exception {
74+
class UndefinedException final : public std::runtime_error {
7575
public:
76-
UndefinedException() : Exception("Undefined operation on game") {}
77-
explicit UndefinedException(const std::string &s) : Exception(s) {}
76+
UndefinedException() : std::runtime_error("Undefined operation on game") {}
77+
explicit UndefinedException(const std::string &s) : std::runtime_error(s) {}
7878
~UndefinedException() noexcept override = default;
7979
};
8080

8181
/// Exception thrown on an operation between incompatible objects
82-
class MismatchException : public Exception {
82+
class MismatchException : public std::runtime_error {
8383
public:
84+
MismatchException() : std::runtime_error("Operation between objects in different games") {}
8485
~MismatchException() noexcept override = default;
85-
const char *what() const noexcept override
86-
{
87-
return "Operation between objects in different games";
88-
}
8986
};
9087

9188
/// Exception thrown when comparing different versions of a game
92-
class GameStructureChangedException : public Exception {
89+
class GameStructureChangedException : public std::runtime_error {
9390
public:
94-
~GameStructureChangedException() noexcept override = default;
95-
const char *what() const noexcept override
91+
GameStructureChangedException()
92+
: std::runtime_error("Game structure has changed since object was defined")
9693
{
97-
return "Game structure has changed since object was defined";
9894
}
95+
~GameStructureChangedException() noexcept override = default;
9996
};
10097

10198
/// Exception thrown on a parse error when reading a game savefile
102-
class InvalidFileException : public Exception {
99+
class InvalidFileException : public std::runtime_error {
103100
public:
104-
InvalidFileException() : Exception("File not in a recognized format") {}
105-
explicit InvalidFileException(const std::string &s) : Exception(s) {}
101+
InvalidFileException() : std::runtime_error("File not in a recognized format") {}
102+
explicit InvalidFileException(const std::string &s) : std::runtime_error(s) {}
106103
~InvalidFileException() noexcept override = default;
107104
};
108105

src/games/gameagg.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ GameStrategy GameAGGRep::GetStrategy(int p_index) const
219219
p_index -= player->GetStrategies().size();
220220
}
221221
}
222-
throw IndexException();
222+
throw std::out_of_range("Strategy index out of range");
223223
}
224224

225225
//------------------------------------------------------------------------

0 commit comments

Comments
 (0)