[C++] Extend IndexError's logic for empty arrays

This commit is contained in:
ThorstenZirwes 2025-02-05 13:57:47 +01:00 committed by Ingmar Schoegl
parent dc018102fd
commit fd1a4103c6
2 changed files with 6 additions and 1 deletions

View File

@ -180,7 +180,8 @@ public:
* @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) {}

View File

@ -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_);
}