Skip to content

Commit

Permalink
[C++] Extend IndexError's logic for empty arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
g3bk47 authored and ischoegl committed Feb 5, 2025
1 parent dc01810 commit fd1a410
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
3 changes: 2 additions & 1 deletion include/cantera/base/ctexceptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ class IndexError : public CanteraError
* @param arrayName name of the corresponding array
* @param m This is the value of the out-of-bounds index.
* @param mmax This is the maximum allowed value of the index. The
* minimum allowed value is assumed to be 0.
* minimum allowed value is assumed to be 0. The special
* value npos indicates that the array is empty.
*/
IndexError(const string& func, const string& arrayName, size_t m, size_t mmax) :
CanteraError(func), arrayName_(arrayName), m_(m), mmax_(mmax) {}
Expand Down
4 changes: 4 additions & 0 deletions src/base/ctexceptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ string ArraySizeError::getMessage() const

string IndexError::getMessage() const
{
if (mmax_ == npos) {
return fmt::format("IndexError: index {} given, but array{} is empty.",
m_, arrayName_.empty() ? arrayName_ : " "+arrayName_);
}
if (arrayName_ == "") {
return fmt::format("IndexError: {} outside valid range of 0 to {}.", m_, mmax_);
}
Expand Down

0 comments on commit fd1a410

Please sign in to comment.