Skip to content

Instantly share code, notes, and snippets.

@XoLinA
Created December 2, 2025 08:04
Show Gist options
  • Select an option

  • Save XoLinA/763f77f149432d9d7b712a7910d19696 to your computer and use it in GitHub Desktop.

Select an option

Save XoLinA/763f77f149432d9d7b712a7910d19696 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
using namespace std;
class ArraySizeException
{
string mess;
public:
ArraySizeException(const std::string& msg) : mess(msg) {}
const char* what() const { return mess.c_str(); }
};
class ArrayDataException
{
string mess;
public:
ArrayDataException(const std::string& msg) : mess(msg) {}
const char* what() const { return mess.c_str(); }
};
class ArrayValueCalculator
{
public:
int doCalculate(const std::string arr[], int size)
{
if (size != 16)
{
throw ArraySizeException("Found: " + std::to_string(size));
}
int sum = 0;
for (int i = 0; i < size; ++i)
{
try
{
int value = std::stoi(arr[i]);
sum += value;
}
catch (...)
{
throw ArrayDataException("Error (" + std::to_string(i) + "): '" + arr[i] + "'");
}
}
return sum;
}
};
int main()
{
string correctArray[16] =
{
"145","243","43","04",
"55","6","3","9",
"10","16","19","62",
"3","4","5","6"
};
string wrongDataArray[16] =
{
"145","243","N","04",
"55","6","3","9",
"10","16","19","62",
"3","4","5","6"
};
ArrayValueCalculator cal;
try
{
int result = cal.doCalculate(correctArray, 16);
cout << "Sum : " << result << endl;
}
catch (const ArraySizeException& e)
{
cout << "Error : " << e.what() << endl;
}
catch (const ArrayDataException& e)
{
cout << "Error: " << e.what() << endl;
}
try
{
int result = cal.doCalculate(wrongDataArray, 16);
cout << "Sum : " << result << endl;
}
catch (const ArraySizeException& e)
{
cout << "Error : " << e.what() << endl;
}
catch (const ArrayDataException& e)
{
cout << "Error: " << e.what() << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment