[fpc-pascal] exiting a program more friendly way

Adrian Maier adrian.maier at gmail.com
Fri Jun 23 09:29:23 CEST 2006


On 23/06/06, Michael Van Canneyt <michael at freepascal.org> wrote:
>
>
> On Fri, 23 Jun 2006, Pianoman wrote:
>
> >
> > Hi, I'd like to ask whether is it possible to exit program in case of runtime
> > error 202 for example more nicely than with message runtime error 202 at
> > a1d456d5621blablabla
> > For example:
> > writeln('starting to divide:');
> > writeln(2/0);
> > and I don't want mi program to stop with runtime error but I 'd like to write
> > to screen something like: writeln('program has atempted to divide by zero
> > enter another expression.');
> > old tp had a special procedure exitproc which allowed to set which code
> > should be executed before terminating in case of run-time error. I mean code
> > which will close opened files or something like that or it could write to
> > logfile the reason of error.
>
> FPC has the same exitproc. See the manuals.

Or, you could use exceptions :

program A;
uses sysutils;
var x:integer;
begin
    x:=0;
    try
        writeln('attempting to divide by zero:');
        writeln(2/x);
        writeln('this line will not be displayed');
    except on E : Exception do
        writeln('Friendly error message  : ',e.message);
    end;
    writeln(' End of program. ');
end.

This way, the program will in fact not crash at all.  If anything nasty
happens between 'try' and 'except',  it will execute the code written
in the except clause. Then,  the program continues normally.


Cheers,
Adrian Maier



More information about the fpc-pascal mailing list