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!
SmallInt. It gave the output as -31536. So in other words, it overflows but not run into the exception-handling code.