#include <iostream>
#include <stdexcept> // For std::overflow_error
// Function to multiply two integers and check for overflow
int multiply(int a, int b) {
// We will check for overflow conditions
if (a > 0 && b > 0 && a > (INT32_MAX / b)) {
throw std::overflow_error("Overflow occurred while multiplying positive numbers");
}
if (a < 0 && b < 0 && a < (INT32_MAX / b)) {
throw std::overflow_error("Overflow occurred while multiplying two negative numbers");
}
if (a > 0 && b < 0 && b < (INT32_MIN / a)) {
throw std::overflow_error("Overflow occurred while multiplying positive by negative number");
}
if (a < 0 && b > 0 && a < (INT32_MIN / b)) {
throw std::overflow_error("Overflow occurred while multiplying negative by positive number");
}
return a * b;
}
int main() {
int x, y;
std::cout << "Enter two integers: ";
std::cin >> x >> y;
try {
int result = multiply(x, y);
std::cout << "Result: " << result << std::endl;
} catch (const std::overflow_error& e) {
std::cerr << "Caught an overflow_error exception: " << e.what() << std::endl;
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RkZXhjZXB0PiAgLy8gRm9yIHN0ZDo6b3ZlcmZsb3dfZXJyb3IKCi8vIEZ1bmN0aW9uIHRvIG11bHRpcGx5IHR3byBpbnRlZ2VycyBhbmQgY2hlY2sgZm9yIG92ZXJmbG93CmludCBtdWx0aXBseShpbnQgYSwgaW50IGIpIHsKICAgIC8vIFdlIHdpbGwgY2hlY2sgZm9yIG92ZXJmbG93IGNvbmRpdGlvbnMKICAgIGlmIChhID4gMCAmJiBiID4gMCAmJiBhID4gKElOVDMyX01BWCAvIGIpKSB7CiAgICAgICAgdGhyb3cgc3RkOjpvdmVyZmxvd19lcnJvcigiT3ZlcmZsb3cgb2NjdXJyZWQgd2hpbGUgbXVsdGlwbHlpbmcgcG9zaXRpdmUgbnVtYmVycyIpOwogICAgfQogICAgaWYgKGEgPCAwICYmIGIgPCAwICYmIGEgPCAoSU5UMzJfTUFYIC8gYikpIHsKICAgICAgICB0aHJvdyBzdGQ6Om92ZXJmbG93X2Vycm9yKCJPdmVyZmxvdyBvY2N1cn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