2

I'm trying this simple program to catch exceptions.

The division by zero exception is successfully caught. However, I cannot seem to catch the integer overflow.

I understand that a SmallInt type integer would overflow after +32767 since the largest signed value = 2^15 - 1 (i.e., +32767).

Here's the code:

Program ss;

Var

  Price, Qty: SmallInt;
  Rate: Real;

Begin
  Try

    write('Enter Total price '); Readln(Price);
    Write('Enter qty '); Readln(Qty);

    Rate := Price / Qty;
    Writeln('Single item = ', Rate:0:2);

  except
    On EDivByZero Do Writeln('qty must be a valid num above 0');
    On EIntOverflow Do Writeln('too large')
  end;
End.

However, it appears that the overflow exception is not caught. Rather, the overflowing value is taken as a negative number and the code continues as if nothing is wrong.

Here's the output:

Enter Total price 32768

Enter qty 1

Single item = -32768.00

Press any key to return to IDE

Am I doing something wrong in the code? What is the correct method of catching the integer overflow in FreePascal?

Thanks!

4
  • I'm not an expert in Free Pascal but my guess is that the overflow exception is only triggered as a result of a calculation. Dividing by 1 doesn't change the value so it doesn't generate an exception. Try adding or subtracting or multiplying values that are in range but the result of the operation is out of range. For example x := 32767; x := x+1 Commented Jan 7, 2016 at 5:20
  • @Stuart Thanks for your input. I tried multiplying 17000 by 2 and storing it in a SmallInt. It gave the output as -31536. So in other words, it overflows but not run into the exception-handling code. Commented Jan 7, 2016 at 5:42
  • Why don't you try the code I suggested. 17000 * 2 will probably be performed as integer multiplication, and when you store the result in a smallint, it is then truncated to a smallint Truncating an integer is not a calculation and so it might not generate a integer overflow exception. Commented Jan 7, 2016 at 6:20
  • @Stuart For your example, the output is -32768 (which is the overflowing value) and no exception is triggered. I recall some older language (I think it was VB6 but I cannot confirm this right now) giving a similar result. Thanks again. Commented Jan 7, 2016 at 9:44

1 Answer 1

1

You must include unit sysutils, as it contains the code that changes the deepest kind of runtime errors into catchable language exceptions

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Marco. Apart from including this unit, is there anything I need to do? I've included it and it works without any change.
For what it's worth, my compiler version shows 2.6.2-8 on the Help > About menu

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.