关于freepascal:异常处理代码中的Illegal Expression error

Illegal Expression error in exception handling code

我正在编写一个小程序来计算 FreePascal 中的交通罚款。源码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
    program TrafficFine;

{$mode objfpc}{$H+}

uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Classes,SysUtils;

var
    userInput : Char;
    Fine      : Integer;
    TotalFine : Integer;
    DaysPassed: Integer;
    FineType  : Integer;

begin

    userInput := 'y';  

    while (userInput = 'Y') or (userInput = 'y') do
    begin;
        writeln('Enter type of fine:');
        writeln('- Enter 1 for not wearing a seat-belt.');
        writeln('- Enter 2 for driving without a license');
        writeln('- Enter 3 for over-speeding.');

        try
            write('Enter value: ');
            readln(FineType);
            if(FineType <0) or (FineType>3) then
                raise exception.Create('Fine type outside of range.');
            case FineType of
            1:  Fine:= 500;
            2:  Fine:= 1000;
            3:  Fine:= 2000;
        except
        on e: exception do {line 39}
        begin
            Writeln('Error: '+e.message);
            continue;
        end;

        write('Enter number of days passed since fine: ');
        readln(DaysPassed);
        if daysPassed<=10 then
            TotalFine := Fine;
        else if (daysPassed >10) and (daysPassed <=30) then
            TotalFine := Fine * 2;
        else
            TotalFine := Fine*2 + Fine*0.5;

        writeln('Total Fine is ' + IntToStr(TotalFine));        
        writeln('Would you like to calculate another fine: ');
        readln(userInput);  
    end;
end.

我收到以下错误:

Free Pascal Compiler version 2.4.4-2ubuntu1 [2011/09/27] for i386
Copyright (c) 1993-2010 by Florian Klaempfl Target OS: Linux for i386
Compiling /home/ubuntu/Desktop/TrafficFine.pas TrafficFine.pas(39,3)
Error: Illegal expression TrafficFine.pas(40,3) Error: Constant
Expression expected TrafficFine.pas(40,3) Fatal: Syntax error,":"
expected but"identifier ON" found Fatal: Compilation aborted

我直接从一本书中学习了这个例子,所以我不确定我哪里出错了。任何帮助,将不胜感激。谢谢。


您的代码中有几个缺陷,我在源代码中进行了更正和评论。试试这个新版本。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
program TrafficFine;

{$mode objfpc}{$H+}

uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Classes,SysUtils;

var
    userInput : Char;
    Fine      : Integer;
    TotalFine : Integer;
    DaysPassed: Integer;
    FineType  : Integer;

begin

    userInput := 'y';

    while (userInput = 'Y') or (userInput = 'y') do
    begin //removed semicolon
        writeln('Enter type of fine:');
        writeln('- Enter 1 for not wearing a seat-belt.');
        writeln('- Enter 2 for driving without a license');
        writeln('- Enter 3 for over-speeding.');

        try
            write('Enter value: ');
            readln(FineType);
            if(FineType <0) or (FineType>3) then
                raise exception.Create('Fine type outside of range.');
            case FineType of
            1:  Fine:= 500;
            2:  Fine:= 1000;
            3:  Fine:= 2000;
            end;//added end;
        except
        on e: exception do {line 39}
        begin
            Writeln('Error: '+e.message);
            continue;
        end;
        end; //added end;

        write('Enter number of days passed since fine: ');
        readln(DaysPassed);
        if daysPassed<=10 then
            TotalFine := Fine //removed semicolon
        else if (daysPassed >10) and (daysPassed <=30) then
            TotalFine := Fine * 2 //removed semicolon
        else
            TotalFine := (Fine*2) + (Fine div 2);//replaced this sentence (Fine*2) + (Fine*0.5)

        writeln('Total Fine is ' + IntToStr(TotalFine));
        writeln('Would you like to calculate another fine: ');
        readln(userInput);
    end;
end.


您好像忘记了用 End 结束案例;