Salute a tutti, sto seguendo un manuale per apprendere il free pascal; ho un problema con le funzioni Try Except e Try Finally, in quanto il programma si blocca nonstante la protezione; qualcuno mi potrebbe aiutare? Grazie
questo non va
program ExceptionHandling;
{$mode objfpc}{$H+}
uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Classes
{ you can add units after this };
var
x, y: Integer;
Res: Double;
begin
try
Write('Input x: ');
Readln(x);
Write('Input y: ');
Readln(y);
Res:= x / y;
Writeln('x / y = ', Res);
finally
Write('Press enter key to close');
Readln;
end;
end.
e nemmeno questo
cthreads,
{$ENDIF}{$ENDIF}
Classes, sysutils
{ you can add units after this };
var
x, y: Integer;
Res: Double;
begin
try
Write('Input x: ');
Readln(x);
Write('Input y: ');
Readln(y);
Res:= x / y;
Writeln('x / y = ', Res);
except
on e: exception do
begin
Writeln('An error occurred: ', e.message);
end;
end;
Write('Press enter key to close');
Readln;
end.
infatti se do x=6 e y=0 mi da quanto nell'immagine(http://)
ciao Nicola
è molto strano, il codice (sto parlando della seconda versione del codice, quella con try...except, il primo è giusto che interrompa l'esecuzione) l'ho provato su linux 64bit e funziona a dovere; il risultato è il seguente:
test $ ./project1
Input x: 6
Input y: 0
An error occurred: Division by zero
Press enter key to close
prova a cancellare tutti i file della compilazione, il codice è corretto