Hi.

First of all, I'd like to apologize for I really do not know where to post this thread to. :lol: I know Pascal is out but apparently I have got to pick up this language in order to finish my project.

What I'd like to ask is whether Pascal offers exception handling. I'd like to read an integer but I've to consider the chances of users keying in a non-integer value. I've checked different sites which offer free online tutorial but to no avail. Please help. Thank you.

Dani AI

Generated

Short answer: treat input as text and validate it before converting. That avoids compiler-dependent behavior when doing readln straight into an Integer. ’s original program reads directly into an integer, which can misbehave on some Pascal systems; ’s suggestion to read a string and convert is the most portable fix, and is right that full exception support (try..except) exists in Delphi/Object Pascal but not in every Pascal dialect. ’s note about real only changes allowed characters, it doesn’t validate that the entry was intended as an integer.

A portable conversion using the classic val routine (Turbo Pascal, Free Pascal, many dialects) looks like this:

var
  s: string;
  n, code: integer;
begin
  write('Enter integer: ');
  readln(s);
  val(s, n, code);
  if code <> 0 then
    writeln('Invalid integer at position ', code)
  else
    writeln('Accepted integer: ', n);
end.

Notes and practical cautions: for Delphi/FPC prefer TryStrToInt (returns boolean) or StrToInt inside try..except where available; val is the fallback on older or ISO Pascal implementations. Always trim whitespace and explicitly reject decimals or signs if the program needs only non‑negative integers.

Factorial-specific warnings: factorials overflow quickly. On typical modern Pascal where Integer is 32‑bit, 12! fits (479001600) but 13! overflows 32‑bit signed. On older 16‑bit Integer implementations even 8! (40320) overflows. Limit input, check negatives, and consider 64‑bit or big‑integer libraries or iterative checks to avoid recursion/overflow problems.

Recommended Answers

All 7 Replies

Hello,

I am not certain if this thread is alive or not, but if you are still out there, Please post your code and let's look at this. I enjoyed Pascal a lot in college, and wish I could still use it instead of C/C++ when I have to occasionally code.

Christian

Greetings.
A thousand apologies.
I was browsing through some old threads and I accidentally bump into my own old thread. :cheesy:
I had eventually gave up on exception handling because I still cannot figure out the way. However, if you know, I'd like to learn it. Thanks a lot.

Program FactorialCalculator;

uses crt;
var input : integer;

function calculate(num:integer):integer;
begin
if ((num = 0) or (num = 1)) then
calculate := 1
else
calculate := calculate(num-1) * num;
end;

begin
writeln;
writeln;
writeln('*** FACTORIAL CALCULATOR ***');
writeln('Enter integer: ');
readln(input);
writeln('The factorial of ',input,' is ',calculate(input));

end.

That was a small program that I got to write using Pascal as a starter.
I'd like to handle the exception where the prompt for an input from the user goes. I only want an integer here.
Any idea?

Hello,

Unfortunately, my Pascal things are in another city. Grr. What happens if you input a letter? Does the program substitute the ASCII value of the letter instead? Hmm. I am wondering if you should accept a string, and then convert it to numeric. I have to see if that function is even available in Pascal.

Wow, I am out of date here.

Christian

Greetings.

Alright. It's ok. I'm just glad that after so long, there is actually a reply from someone.

I'm in my office right now ( Industrial Training :sad: ). I'll check with my pascal compiler once I get home later in the night.

What happens if you input a letter? Does the program substitute the ASCII value of the letter instead?

Thanks anyway, Christian.

hey, im a pascal starter too. instead of this:

var input : integer;

try using this :

var input :real;

yea. i just learned that last week.

integer is a whole number
real is a whole or decimal. but if you put a decimal, REMEMBER to enter it like so: 0.6

program FactorialCalculator;

{$APPTYPE CONSOLE}

uses
   SysUtils;

var
   input : string;
   value : Integer;

function calculate( num : integer ) : integer;
begin
   if ( ( num = 0 ) or ( num = 1 ) ) then
      calculate := 1
   else
      calculate := calculate( num - 1 ) * num;
end;

begin
   writeln;
   writeln;
   writeln( '*** FACTORIAL CALCULATOR ***' );
   repeat
      write( 'Enter integer: ' );
      readln( input );
   until TryStrToInt( input, value );
   writeln( 'The factorial of ', input, ' is ', calculate( value ) );
end.

Whether you get exception handling or not depends on the flavour of Pascal you're working with.

Delphi (which is Pascal with a nice IDE and stuff) for example has a comprehensive exception mechanism (though it might not have been available in the very early versions, I don't have those here to check).

Other dialects may have duplicated this over time (Borland being the de-facto standard for Pascal in lue of an official standards body).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.