Illegal Expression error in exception handling code - exception

I'n writing a small program to calculate traffic fines in FreePascal. The source code is as follows:
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.
I get the following errors:
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
I followed the example straight from a book so I'm not sure where I've gone wrong. Any help would be appreciated. Thanks.

You have several flaws in your code, I corrected and commented in the source. try this new version.
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.

It seems like you forgot to close Case with an End;

Related

Detect office 365 (access) AND Visual C 64x using registry

I have a MS Access app which I am planning to sell to people however it uses ODBC (MYSQL) to connect to a backend MySQL DB plus obviously needs ms access (runtime at least).
I created an Inno Setup installer to check for installed components however I have come across some issues:
ODBC 64x fails unless you also have ODBC 32x installed
ODBC (either) cant install unless the respective bit Visual C++ 2015-2019 is installed
My registry path for Visual C+ detects the install but not the Bit version!
Ms Access 365 (2019?) isn't detected using my current registry detection methods.
Code used:
function GetHKLM: Integer;
begin
if IsWin64 then
Result := HKLM64
else
Result := HKLM32;
Result := HKLM;
end;
function IsOfficeInstalled: Boolean;
begin
Result := RegKeyExists(GetHKLM, 'Software\Microsoft\Office');
end;
function NormalAccessPath: String;
var
Names: TArrayOfString;
I: Integer;
S: String;
begin
if not IsOfficeInstalled then
Result:=''
else
begin
if RegGetSubkeyNames(GetHKLM, 'Software\Microsoft\Office', Names) then
begin
for I := 0 to GetArrayLength(Names)-1 do
begin
S := 'Software\Microsoft\Office\' + Names[I]+ '\' + 'Access';
if RegKeyExists(GetHKLM, S) then
Result:=S;
S := '';
end
end
end;
end;
function IsNormalAccessInstalled: Boolean;
var
Path: String;
begin
Path:= NormalAccessPath;
if Path <> '' then
Result := True
else
Result := False
end;
function IsMySQLODBC51Installed: Boolean;
begin
// the result was inverted in the original code; the original function returned
// True if the ODBC driver was not installed, False otherwise, and according to
// the function name it should be vice-versa
Result := RegKeyExists(GetHKLM, 'Software\ODBC\ODBCINST.INI\MySQL ODBC 8.0 Unicode Driver');
end;
function IsRuntimeAccessInstalled: Boolean;
begin
Result := RegKeyExists(GetHKLM, 'Software\Microsoft\Windows\CurrentVersion\Uninstall\AccessRuntimeRetail - en-us');
end;
function IsVCInstalled: Boolean;
begin
Result := RegKeyExists(GetHKLM, 'Software\Microsoft\VisualStudio\14.0\VC');
end;
function AddAsTrusted: Boolean;
var
Path: String;
ResultCode: Integer;
begin
Path:= NormalAccessPath;
MsgBox('Adding Trusted Location', mbInformation, MB_OK);
//Exec('C:\Me\freelance\Inno setup script\fixes.bat', '', '', SW_SHOW, ewWaitUntilTerminated, ResultCode)
if Path <> '' then
begin
//if not RegKeyExists(GetHKLM, Path + '\Security') then
// RegWriteStringValue(GetHKLM, Path + '\Security','MedidropUseless', 'MedidropUseless')
if not RegKeyExists(GetHKLM, Path + '\Security\Trusted Locations') then
RegWriteStringValue(GetHKLM, Path + '\Security\Trusted Locations\Location1','Path', 'C:\MediDrop')
end;
Result := True;
end;
So for 2 & 3:
I need to detect visualC's VERSION.
I tested the installer at a new location today and they already had visual C++ 2015 86x so my installed failed for ODBC 64x because visualC 64x was missing. Obviously:
'Software\Microsoft\VisualStudio\14.0\VC'
is not appropriate..
For 4:
As can be seen I am checking Software\Microsoft\Office however this computer did have the 15.0 and 16.0 subfolders (terminology?) but did NOT have ACCESS in them so my installer thought it was not installed.
However.. it was, so I need to find a way of finding this 365 version of access!
Finally, at the end of the install I add a path for trusted location:
function AddAsTrusted: Boolean;
var
Path: String;
ResultCode: Integer;
begin
Path:= NormalAccessPath;
MsgBox('Adding Trusted Location', mbInformation, MB_OK);
//Exec('C:\Me\freelance\Inno setup script\fixes.bat', '', '', SW_SHOW, ewWaitUntilTerminated, ResultCode)
if Path <> '' then
begin
if not RegKeyExists(GetHKLM, Path + '\Security\Trusted Locations') then
RegWriteStringValue(GetHKLM, Path + '\Security\Trusted Locations\Location1','Path', 'C:\[MyAppName]')
end;
Result := True;
end;
Once again due to the 365 issue... this did not work. I created the path correctly in 16.0 but since access wasnt there, I still needed to Enable Content when I opened the app the first time, something I don't want to do.
Does anyone know where I can start to figure the above issues out??

Why TFDBatchMove raises exception ELocalTimeInvalid for date field with value "03/11/2019"?

I'm using Delphi 10.3 Rio Update 1 on a Windows 7 SP1 machine.
My program's purpose is to convert a TFDMemtable into a JSON format. For a reason that I can't understand, when the date field of this TFDMemTable has the value '03/11/2019', using the DisplayFormat "day/month/year", it raises an exception:
Project ProjMemtabJSON.exe raised exception class ELocalTimeInvalid with message 'The given "03/11/2019" local time is invalid (situated within the missing period prior to DST).'.
Any other dates different than "Nov, 3rd 2019" work fine.
I have no clue what is going on here!
program ProjMemtabJSON;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils,
System.Classes,
System.JSON,
FireDAC.Comp.DataSet,
FireDAC.Comp.Client,
FireDAC.Comp.BatchMove,
FireDAC.Comp.BatchMove.DataSet,
FireDAC.Comp.BatchMove.JSON,
Data.DB;
Var
Fmemtable : TFDmemtable ;
FJSONArray : TJSONArray;
FDBatchMoveJSONWriter1 : TFDBatchMoveJSONWriter;
FDBatchMoveDataSetReader1 : TFDBatchMoveDataSetReader;
FDBatchMove1 : TFDBatchMove;
procedure CreateMemtable;
begin
Fmemtable := TFDmemtable.Create(nil);
FMemTable.FieldDefs.Add('ID', ftInteger, 0, False);
FMemTable.FieldDefs.Add('Name', ftString, 20, False);
FMemTable.FieldDefs.Add('Status', ftString, 20, False);
FMemTable.FieldDefs.Add('Duedate', ftdatetime,0, False);
FMemTable.CreateDataSet;
end;
procedure FillMemtable;
begin
FMemtable.Append;
FMemtable.Fields[0].Value := 10; // ID
FMemtable.Fields[1].Value := 'John'; // Name
FMemtable.Fields[2].Value := 'Active'; // Status
{ ==> HERE IS THE PROBLEM : change this date to 03/11/2019 i.e. 03/Nov/2019 and an error will raise }
FMemtable.Fields[3].Value := StrToDate('02/11/2019'); // Due date dd/mm/yyyy
end;
procedure PrintMemtable;
begin
writeln('ID : ' ,Fmemtable.Fields[0].AsString);
writeln('Name : ' ,Fmemtable.Fields[1].AsString);
writeln('Status : ' ,Fmemtable.Fields[2].AsString);
writeln('Due Date : ' ,Fmemtable.Fields[3].AsString);
end;
function TableToJson : TJSONArray;
begin
Result := TJSONArray.Create;
try
FDBatchMoveDataSetReader1 := TFDBatchMoveDataSetReader.Create(nil);
FDBatchMoveJSONWriter1 := TFDBatchMoveJSONWriter.Create(nil);
FDBatchMove1 := TFDBatchMove.Create(nil);
FDBatchMove1.Reader := FDBatchMoveDataSetReader1 ;
FDBatchMove1.Writer := FDBatchMoveJSONWriter1;
try
if not FMemtable.Active then
FMemtable.Active := True;
FDBatchMoveDataSetReader1.DataSet := FMemtable;
FDBatchMoveJSONWriter1.JsonArray := Result;
FDBatchMove1.Execute;
except
on E: Exception do
raise Exception.Create('Error Message: ' + E.Message);
end;
finally
FDBatchMoveDataSetReader1.Free;
FDBatchMoveJSONWriter1.Free;
FDBatchMove1.Free;
end;
end;
begin
try
{ TODO -oUser -cConsole Main : Insert code here }
Creatememtable;
FillMemtable;
PrintMemtable;
FJSONArray := TableToJSON;
readln;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.
There is a System.DateUtils routine that can check for an invalid time:
TTimeZone.IsInvalidTime(const ADateTime : TDateTime) : Boolean;
If you add DateUtils to your Uses clause, then update your FillMemTable to something like:
procedure FillMemtable;
var MyDate : tDateTime;
begin
FMemtable.Append;
FMemtable.Fields[0].Value := 10; // ID
FMemtable.Fields[1].Value := 'John'; // Name
FMemtable.Fields[2].Value := 'Active'; // Status
{ ==> HERE IS THE PROBLEM : change this date to 03/11/2019 i.e. 03/Nov/2019 and an error will raise }
MyDate := StrToDate('03/11/2019');
if TTimeZone.local.IsInvalidTime(MyDate) then MyDate := MyDate + 0.5; //noon won't be invalid
FMemtable.Fields[3].Value := MyDate; // Due date dd/mm/yyyy
end;
Or, as mentioned in the comments, if you don't want the overhead of the IF statement, just force all dates to be noon.
I had never realized that there were time zones which switched to/from DST at midnight. Unfortunate, what with how Dates (without times) are defined.

Delphi 2010 : UniDAC vs Indy-MultiThread safety handleing method

I am doing develop Indy based application.
Server has several Indy TCP Server components.
So It works under multi-threads and handles mysql db.
I have faced one problem.
That is about the exceptions of MySQL DB in threads.
When serveral threads attack to same db table, then It says me like follows
UniQuery_Mgr: Duplicate field name 'id'
UniQuery_Mgr: Field 'grp_id' not found //of course grp_id field is really existed.
Assertion failure (C:\Program Files (x86)\unidac539src\Source\CRVio.pas, line 255)
Commands out of sync; You can't run this command now
ReceiveHeader: Net packets out of order: received[0], expected[1]
UniQuery_Mgr: Cannot perform this operation on a closed dataset
How to do I ? UniQuery_Mgr is TUniQuery component.
and my query handling code is normally like this
Code 1
sql := 'SELECT * FROM data_writed;';//for example
UniQuery_Mgr.SQL.Clear;
UniQuery_Mgr.SQL.Add(sql);
UniQuery_Mgr.ExecSQL;
Code 2
try
sql := 'SELECT * FROM gamegrp_mgr;';
UniQuery_Mgr.SQL.Clear;
UniQuery_Mgr.SQL.Add(sql);
UniQuery_Mgr.ExecSQL;
if UniQuery_Mgr.RecordCount > 0 then
begin
MAX_GAME_GROUP_COUNT := UniQuery_Mgr.RecordCount + 1;
UniQuery_Mgr.First;
i := 1;
while not UniQuery_Mgr.Eof do
begin
Game_Group_ID[i] := UniQuery_Mgr.FieldByName('grp_id').AsInteger;
Game_Game_ID[i] := UniQuery_Mgr.FieldByName('game_id').AsInteger;
UniQuery_Mgr.Next;
Inc(i);
end;
end;
except
on E : Exception do
begin
EGAMEMSG := Format('GAME group read error: <%s> # %s',[ E.ToString, DateTimeToStr(now)]);
Exit;
end;
end;
Code 3
try
sql := 'UPDATE data_writed SET write_gamegrp = ' + QuotedStr('0') + ';';
UniQuery_Mgr.SQL.Clear;
UniQuery_Mgr.SQL.Add(sql);
UniQuery_Mgr.ExecSQL;
except
on E : Exception do
begin
EGAMEMSG := Format('data updating error: <%s> # %s',[ E.ToString, DateTimeToStr(now)]);
Exit;
end;
end;
My handling DB components is bad ? Other thread-safe method is existed???

Getting MySQL process output

I want to play with MySQL process and get what ever it write to console so I write this code in FreePascal:
I want to control MySQl and read & write whatever.
Process := TProcess.Create(nil);
with Process do
begin
Executable := 'C:\Program Files (x86)\MySQL\MySQL Server 5.5\bin\mysql.exe';
with Parameters do
begin
Options := [poUsePipes];
Add('-u');
Add('root');
Add('-p');
end;
Execute;
sleep(1000);
WriteLn(Process.Output.NumBytesAvailable); // will be 0 but it write "Enter password"
WriteLn(Process.Stderr.NumBytesAvailable); // will be 0 but it write "Enter password"
end;
TProcess is a component that control executing other programs,I even test Delphi codes but all result are the same.
But the problem is that this will freeze because there is no output but console window write :
Enter password:
How can I get this in my application and all others?
As I said I want to work with MySQL executable and read from and write in it, So I dont want to use its library or any other DB component.
EDIT:
Here is a Delphi version of my test from here with the same result :
function GetDosOutput(CommandLine: string; Work: string = 'C:\'): string;
var
SA: TSecurityAttributes;
SI: TStartupInfo;
PI: TProcessInformation;
StdOutPipeRead, StdOutPipeWrite: THandle;
WasOK: Boolean;
Buffer: array[0..255] of AnsiChar;
BytesRead: Cardinal;
WorkDir: string;
Handle: Boolean;
begin
Result := '';
with SA do begin
nLength := SizeOf(SA);
bInheritHandle := True;
lpSecurityDescriptor := nil;
end;
CreatePipe(StdOutPipeRead, StdOutPipeWrite, #SA, 0);
try
with SI do
begin
FillChar(SI, SizeOf(SI), 0);
cb := SizeOf(SI);
dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
wShowWindow := SW_HIDE;
hStdInput := GetStdHandle(STD_INPUT_HANDLE); // don't redirect stdin
hStdOutput := StdOutPipeWrite;
hStdError := StdOutPipeWrite;
end;
WorkDir := Work;
Handle := CreateProcess(nil, PChar('cmd.exe /C ' + CommandLine),
nil, nil, True, 0, nil,
PChar(WorkDir), SI, PI);
CloseHandle(StdOutPipeWrite);
if Handle then
try
repeat
WasOK := ReadFile(StdOutPipeRead, Buffer, 255, BytesRead, nil);
if BytesRead > 0 then
begin
Buffer[BytesRead] := #0;
Result := Result + Buffer;
end;
until not WasOK or (BytesRead = 0);
WaitForSingleObject(PI.hProcess, INFINITE);
finally
CloseHandle(PI.hThread);
CloseHandle(PI.hProcess);
end;
finally
CloseHandle(StdOutPipeRead);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
memo1.text:=GetDosOutput('"C:\Program Files (x86)\MySQL\MySQL Server 5.5\bin\mysql.exe" -u root -p');
end;
It will freeze in WasOK line .
Also I tested many other codes for this subject and neither worked.

Inno Setup GetExceptionMessage returns empty message

in Inno Setup script GetExceptionMessage returns empty message (it contains only colon ":" sign). The last version of Inno Setup (5.4.2) is used.
try
Log('Create IISNamespace');
// Create IIS namespace object
if Length(virtualDirectoryName) > 0 then
begin
IIS := CreateOleObject('IISNamespace');
Log('Get IIsWebService');
WebSite := IIS.GetObject('IIsWebService', IISServerName + '/w3svc');
Log('Get IIsWebServer');
WebServer := WebSite.GetObject('IIsWebServer', IISServerNumber);
Log('Get IIsWebVirtualDir');
WebRoot := WebServer.GetObject('IIsWebVirtualDir', 'Root');
Log('Delete IIsWebVirtualDir');
WebRoot.Delete('IIsWebVirtualDir', virtualDirectoryName);
WebRoot.SetInfo();
end;
except
MsgBox(ExpandConstant('{cm:IISException,'+ GetExceptionMessage +'}'),
mbInformation, mb_Ok);
Log('Uninstall IIS 6 exception: ' + GetExceptionMessage);
end;
The exception occurs during deleting IIsWebVirtualDir.
Is there any way to get exception type or real exception message?
Thanks, Denis.
I just wrote the following example to see if either GetExceptionMessage or ShowExceptionMessage are broken. I used both Inno setup 5.4.2 Unicode and Ansi versions.
[Setup]
AppName=Test
AppVersion=1.5
DefaultDirName={pf}\test
[Code]
function InitializeSetup(): Boolean;
var
I: Integer;
begin
try
I := I div 0; // Raise an exception
except
MsgBox(GetExceptionMessage,
mbError, MB_OK);
ShowExceptionMessage;
end;
result := false;
end;
I also ran CodeAutomation.iss that ships and it worked as expected. Which is contrary to the comment made by Alex K. that it may be broken.
Now that I know that the routines should work, I took your code and made the following setup test Script and ran it and it raised an exception on not finding the ISSNamespace as I don't have it installed.
[Setup]
AppName=Test
AppVersion=1.5
DefaultDirName={pf}\test
[CustomMessages]
IISException =ISS Exception " %1 " occured.
[Code]
const
IISServerName = 'localhost';
IISServerNumber = '1';
IISURL = 'http://127.0.0.1';
function InitializeSetup(): Boolean;
var
IIS, WebSite, WebServer, WebRoot, VDir: Variant;
virtualDirectoryName : String;
begin
virtualDirectoryName := 'test';
try
Log('Create IISNamespace');
// Create IIS namespace object
if Length(virtualDirectoryName) > 0 then
begin
IIS := CreateOleObject('IISNamespace');
Log('Get IIsWebService');
WebSite := IIS.GetObject('IIsWebService', IISServerName + '/w3svc');
Log('Get IIsWebServer');
WebServer := WebSite.GetObject('IIsWebServer', IISServerNumber);
Log('Get IIsWebVirtualDir');
WebRoot := WebServer.GetObject('IIsWebVirtualDir', 'Root');
Log('Delete IIsWebVirtualDir');
WebRoot.Delete('IIsWebVirtualDir', virtualDirectoryName);
WebRoot.SetInfo();
end;
except
MsgBox(ExpandConstant('{cm:IISException,'+ GetExceptionMessage +'}'),
mbInformation, mb_Ok);
Log('Uninstall IIS 6 exception: ' + GetExceptionMessage);
end;
end;
But I made a fatal flaw during the construction of this script that could be your problem.
Check your [CustomMesssages] section make sure you have %1 in the message. Otherwise nothing is returned.