我正在尝试使用try ..运行以下代码,除了块:
program TriangleArea;
uses crt, sysutils;
type
num = real;
var
a, b, c, s, area : num;
begin
write('Enter lengths of 3 sides (separated by spaces): ');
try
readln (a, b, c);
s := (a + b + c)/2.0;
area := sqrt(s * (s - a)*(s-b)*(s-c));
writeln(area);
except
on E: Exception do
ShowMessage( 'Error: '+ E.ClassName + #13#10 + E.Message );
end;
end.
但是它给出了以下错误:
$ fpc triangle_area.pas
Free Pascal Compiler version 3.0.0+dfsg-11+deb9u1 [2017/06/10] for x86_64
Copyright (c) 1993-2015 by Florian Klaempfl and others
Target OS: Linux for x86-64
Compiling triangle_area.pas
triangle_area.pas(14,2) Error: Identifier not found "try"
triangle_area.pas(15,3) Fatal: Syntax error, ";" expected but "identifier READLN" found
Fatal: Compilation aborted
Error: /usr/bin/ppcx64 returned an error exitcode
为什么找不到“ try”标识符。我在Debian稳定Linux上使用的fpc版本3.0.0。
问题出在哪里,如何解决?谢谢你的帮助。
答案 0 :(得分:2)
在程序声明下方放置{$MODE OBJFPC}
或{$MODE DELPHI}
。
原因是默认情况下,编译器将在MODE FPC
中进行编译,而该编译器不支持异常。
另一方面,ShowMessage
指令将无法使用Free Pascal进行编译。正确的代码是:
program TriangleArea;
{$mode delphi}
uses crt, sysutils;
type
num = real;
var
a, b, c, s, area : num;
begin
write('Enter lengths of 3 sides (separated by spaces): ');
try
readln (a, b, c);
s := (a + b + c)/2.0;
area := sqrt(s * (s - a)*(s-b)*(s-c));
writeln(area);
except
on E: Exception do
write( 'Error: '+ E.ClassName + #13#10 + E.Message );
end;
end.
稍后编辑:声明type num=real
是正确的,但我看不到它的任何实际用途。