Skip to content
Snippets Groups Projects
Commit 2fb30229 authored by Anna Gillert's avatar Anna Gillert
Browse files

Fix: order of operands in subtraction, error msgs

Improved error messages for integer overflows and division errors
parent 1ea67e06
No related branches found
No related tags found
No related merge requests found
......@@ -13,7 +13,7 @@ int add(int a, int b) {
int res;
bool overflow = __builtin_sadd_overflow(a, b, &res);
if (overflow) {
throw std::invalid_argument("Malformed input. Integer overflow.");
throw std::invalid_argument("Malformed input. Integer overflow (add).");
}
return res;
}
......@@ -35,9 +35,9 @@ int multiply(int a, int b) {
*/
int subtract(int a, int b) {
int res;
bool overflow = __builtin_ssub_overflow(a, b, &res);
bool overflow = __builtin_ssub_overflow(b, a, &res);
if (overflow) {
throw std::invalid_argument("Malformed input. Integer overflow.");
throw std::invalid_argument("Malformed input. Integer overflow (subtract).");
}
return res;
}
......@@ -48,7 +48,7 @@ int subtract(int a, int b) {
*/
int divide(int a, int b) {
if (b == 0) {
throw std::invalid_argument("Tried to divide by 0.");
throw std::invalid_argument("Malformed input. Division by zero.");
}
return a / b;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment