Welcome to LeetCode Discuss.
This is a place to ask questions related to only OJ problems.
Please read the FAQ to help yourself making the best use of Discuss.
Throw an exception? Good, but what if throwing an exception is not an option? You would then have to re-design the function (ie, add an extra parameter).
As a cpp coder, I will use the long long to handle the overflow case. Since all input number is in int range, there is no way to overflow the long long.
long long
int