-6

I want to catch the error if the value exceeds the maximum int range in c++.

try
{
    int num = 123123123123123123123312;
    cout << num;
}
catch(.....)
{

}
  • Do you want to catch an integer overflow at runtime? – Ven Feb 08 '18 at 16:08
  • 3
    Your compiler might warn you. There are no runtime exception for that. – Jarod42 Feb 08 '18 at 16:08
  • Turn up your compiler warnings: http://coliru.stacked-crooked.com/a/4bc15117ce755a62 – NathanOliver Feb 08 '18 at 16:08
  • 5
    Doesn't compile: `error: integer constant is too large for its type` So runtime try/catch is irrelevant. – Richard Critten Feb 08 '18 at 16:09
  • You can't catch errors, only exceptions. And nothing in the problematic line does `throw something;`, so there is no `catch`ing it. – nwp Feb 08 '18 at 16:10
  • Potential dupe: https://stackoverflow.com/q/35138864/3002139 – Baum mit Augen Feb 08 '18 at 16:11
  • I think integer overflow at runtime is what Im searching. Can you give me example sir? – MaeLstroms Stroms Feb 08 '18 at 16:11
  • Highly related: https://stackoverflow.com/q/199333/3002139 – Baum mit Augen Feb 08 '18 at 16:12
  • 1
    signed integer overflow is undefined behavior ... no, you can't catch it (but you can test for it), you can just watch the world fall apart around your program – UKMonkey Feb 08 '18 at 16:12
  • c++ is not the language that holds your hand and catches you if you get into a pitfall. If you want to catch an exception in such a case, you first need to detect the overflow and then you need to throw something – 463035818_is_not_an_ai Feb 08 '18 at 16:13
  • 1
    Possible duplicate of [Is overflow\_error caught at runtime in C++ when a integer variable cannot hold a value that it is not supposed to](https://stackoverflow.com/questions/35138864/is-overflow-error-caught-at-runtime-in-c-when-a-integer-variable-cannot-hold-a) – Iulian Popescu Feb 08 '18 at 16:16
  • @UKMonkey this particular program doesn't have signed integer overlfow however. It has either a conversion to `int` from a larger type in which case value is implementation defined, or the program is ill-formed because the literal is not representable by any integer type. No UB in either case. – eerorika Feb 08 '18 at 16:26
  • @user2079303 when you read the comments, you'll note that the question isn't about the particular program. – UKMonkey Feb 08 '18 at 16:27

1 Answers1

2

I want to catch the error if the value exceeds the maximum int range in c++.

It's not possible to catch anything, because integer conversion is not specified to throw anything. num will simply have an implementation defined value - or if 123123123123123123123312 is too big to be represented by any integer type on the system, then the program is simply ill-formed.

It is possible that a compiler is smart enough to "catch" this bug and warn you about it. In the case where the integer literal cannot be represented by any integer type, the compiler is required to inform you.

I think integer overflow at runtime is what Im searching.

Integer overflow happens when you do an arithmetic operation (such as add or subtract) on signed integer(s) and the result is not representable by the integer type.

It is not possible to "catch" integer overflow either. When integer overflow happens, the behaviour is undefined. You cannot know whether integer overflow has happened, but you can check whether integer overflow would happen:

int a = getvalue();
int b = getvalue();

bool addWouldOverflow = false; // also check for underflow
if((b < 0) && (a < std::numeric_limits<int>::min() - b))
    addWouldOverflow = true;
else if((b > 0) && (a > std::numeric_limits<int>::max() - b))
    addWouldOverflow = true;

if(addWouldOverflow)
    throw std::overflow_error("Oops! input is bad :(");
else
    return a + b;
eerorika
  • 232,697
  • 12
  • 197
  • 326