Recent

Author Topic: 'try' is not found when I do Run, Build File  (Read 3256 times)

bulrush

  • Jr. Member
  • **
  • Posts: 84
'try' is not found when I do Run, Build File
« on: June 14, 2016, 04:12:03 pm »
This is a CLI program with Windows 7 and Lazarus 1.6.0 and FPC 3.0.0. Here's the procedure in my main .pas file.
Code: Pascal  [Select]
  1. //#################################################################
  2. procedure makemimetype();
  3. var fn:string;
  4.     tfOut: TextFile;
  5. begin
  6.  
  7. fn:=gApppath+'mimetype';
  8. if (FileExists(fn)) then
  9.   //begin
  10.   try // Error: "Identifier not found 'try'""
  11.     begin
  12.     AssignFile(tfOut, fn);
  13.     // Create the file, write some text and close it.
  14.     rewrite(tfOut);
  15.     write(tfOut, 'application/epub+zip');
  16.     //ShowMessage('Created required "mimetype" file for you in EXE dir.');
  17.     CloseFile(tfOut);
  18.     end;
  19.   except
  20.     // If there was an error the reason can be found here
  21.     on E: EInOutError do
  22.       writeerr('File handling error occurred. Details: '+ E.ClassName+ '/'+ E.Message);
  23.   end;
  24.   //end;
  25.  
  26. end; // makemimetype
  27.  

I get this error when I do Run, Build File but when I do Run, Build (SH-F9), I get no errors. Here are all the build errors:
Code: Pascal  [Select]
  1. Build File C:\chuck\compiler\lazarus\Projects\mkcli\mkepub.lpr: Exit code 1, Errors: 2
  2. Copyright (c) 1993-2015 by Florian Klaempfl and others
  3. Target OS: Win64 for x64
  4. mkepub.lpr(229,3) Error: Identifier not found "try"
  5. mkepub.lpr(230,5) Error: Syntax error, ";" expected but "BEGIN" found
  6.  

What could be going on here?

Thank you. :)
Lazarus 1.6.0, FPC 3.0.0, Win 7 64-bit, current Perl programmer.

rvk

  • Hero Member
  • *****
  • Posts: 3842
Re: 'try' is not found when I do Run, Build File
« Reply #1 on: June 14, 2016, 04:28:48 pm »
Did you supply a mode for the program ({$MODE DELPHI} or {$MODE OBJFPC} or during compile-time) ??
http://wiki.lazarus.freepascal.org/Code_Conversion_Guide#Selecting_the_right_compiler_mode

You need one of the two to be able to use try/finally or try/except.

And if you're in Lazarus you can check the Project Options > Compiler options > Parsing.
What mode is selected there?

« Last Edit: June 14, 2016, 04:30:50 pm by rvk »

Cyrax

  • Hero Member
  • *****
  • Posts: 757
Re: 'try' is not found when I do Run, Build File
« Reply #2 on: June 15, 2016, 12:44:24 pm »
Remove begin and end; from inside your try..except block.

Thaddy

  • Hero Member
  • *****
  • Posts: 9140
Re: 'try' is not found when I do Run, Build File
« Reply #3 on: June 15, 2016, 12:54:02 pm »
No simply write proper code:
(I do not agree with this style, but alas it works now)
Code: Pascal  [Select]
  1. program Project9;
  2. {$IFDEF FPC}{$MODE OBJFPC}{H$H+}{$ENDIF}
  3. {$APPTYPE CONSOLE}
  4. uses
  5.   sysutils;
  6.  
  7. procedure makemimetype();
  8. var fn:string;
  9.     tfOut: TextFile;
  10. begin
  11.  
  12. fn:='';//gApppath+'mimetype';
  13. if (FileExists(fn)) then
  14.   //begin
  15.   try // Error: "Identifier not found 'try'""
  16.     begin
  17.     AssignFile(tfOut, fn);
  18.     // Create the file, write some text and close it.
  19.     rewrite(tfOut);
  20.     write(tfOut, 'application/epub+zip');
  21.     //ShowMessage('Created required "mimetype" file for you in EXE dir.');
  22.     CloseFile(tfOut);
  23.     end;
  24.   except
  25.     // If there was an error the reason can be found here
  26.     on E: EInOutError do
  27.       writeln('File handling error occurred. Details: '+ E.ClassName+ '/'+ E.Message);
  28.   end;
  29.   //end;
  30.  
  31. end; // makemimetype
  32.  
  33. begin
  34.   try
  35.     { TODO -oUser -cConsole Main : Insert code here }
  36.   except
  37.     on E:Exception do
  38.       Writeln(E.Classname, ': ', E.Message);
  39.   end;
  40. end.
  41.  

To Bulrush:

Put in some effort. Takes two seconds... >:D
also related to equus asinus.