Recent

Author Topic: Try Finally Except - Help  (Read 2841 times)

Tony Stone

  • Jr. Member
  • **
  • Posts: 82
Try Finally Except - Help
« on: July 13, 2021, 12:47:03 am »
I am trying to clean up a lot of my code messes.  I am trying to implement error handling where possible and this is my first go at use try...except... statements.  I have used them, typically what I copied from examples on the net.  I really need my procedure to exit or abort if an exception happens.  Simply calling exit; after the On E: EInOutError do gives me a compile error.  Expects end;

This is a sample of my attempt, can I only have one statement for the on E: do ... statement?
Code: Pascal  [Select][+][-]
  1.   if dlgFMsaveTarget.Execute then
  2.   begin
  3.     // create target file
  4.     try
  5.       AssignFile(TargetFileOD, dlgFMsaveTarget.FileName);
  6.       Rewrite(TargetFileOD);
  7.       CloseFile(TargetFileOD);
  8.     except
  9.       on E: EInOutError do
  10.         ShowMessage('File handling error occurred. Details: ' +
  11.           E.ClassName + '/' + E.Message);
  12.        //exit; // there was an error... need to get out of entire procedure
  13.     end;
  14.   end else exit; //user pressed cancel in dialog - get out of here
  15.  startProcessing;
  16.  
  17.  

Fantablup

  • Full Member
  • ***
  • Posts: 140
Re: Try Finally Except - Help
« Reply #1 on: July 13, 2021, 12:50:45 am »
          except
            on E: EDatabaseError do
            begin
              ShowMessage( 'Database error: '+ E.ClassName + #13#10 + E.Message );
              ShowMessage( 'hey dude ');
            end;
            on E: Exception do
            begin
              ShowMessage( 'Error: '+ E.ClassName + #13#10 + E.Message );
            end;
          end;
« Last Edit: July 13, 2021, 12:54:30 am by Fantablup »

sstvmaster

  • Sr. Member
  • ****
  • Posts: 259
Re: Try Finally Except - Help
« Reply #2 on: July 13, 2021, 12:56:07 am »
.
..Simply calling exit; after the On E: EInOutError do gives me a compile error.  Expects end;

...
Code: Pascal  [Select][+][-]
  1. ...
  2.     except
  3.       on E: EInOutError do
  4.         begin // <--
  5.           ShowMessage('File handling error occurred. Details: ' + E.ClassName + '/' + E.Message);
  6.           exit;
  7.         end; // <--
  8.     end;
  9. ...
Windows 10 (64 bit)
Lazarus: 2.0.12 / Trunk x32

Tony Stone

  • Jr. Member
  • **
  • Posts: 82
Re: Try Finally Except - Help
« Reply #3 on: July 13, 2021, 01:06:37 am »
DOH!!!!   :-[


I feel I need to delete this post now!  I am embarrassed.  Thank you!  Or i'll just leave it for other newbies...

Grrr!  This was a simple lesson with if then statements.  I really canot beleive I posted this question in public... lol
.
..Simply calling exit; after the On E: EInOutError do gives me a compile error.  Expects end;

...
Code: Pascal  [Select][+][-]
  1. ...
  2.     except
  3.       on E: EInOutError do
  4.         begin // <--
  5.           ShowMessage('File handling error occurred. Details: ' + E.ClassName + '/' + E.Message);
  6.           exit;
  7.         end; // <--
  8.     end;
  9. ...
« Last Edit: July 13, 2021, 01:09:10 am by Tony Stone »

Fantablup

  • Full Member
  • ***
  • Posts: 140
Re: Try Finally Except - Help
« Reply #4 on: July 13, 2021, 01:25:18 am »
Nothing to be embarrassed about.
We all have been there :)

PascalDragon

  • Hero Member
  • *****
  • Posts: 3335
  • Compiler Developer
Re: Try Finally Except - Help
« Reply #5 on: July 13, 2021, 09:05:14 am »
          except
            on E: EDatabaseError do
            begin
              ShowMessage( 'Database error: '+ E.ClassName + #13#10 + E.Message );
              ShowMessage( 'hey dude ');
            end;
            on E: Exception do
            begin
              ShowMessage( 'Error: '+ E.ClassName + #13#10 + E.Message );
            end;
          end;

Please use [ code ] tags when providing code as this way it's easier recognizable, provides a separate scroll region if it's large and also avoids the forum software possibly messing up parts due to its BBcode handling.

dbannon

  • Hero Member
  • *****
  • Posts: 1537
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Try Finally Except - Help
« Reply #6 on: July 13, 2021, 09:34:50 am »
And while we are being picky, much better to use the const, LineEnding instead of #13#10, it will work on all platforms, not just Windows.

 :)

Davo
Lazarus 2, Linux (and reluctantly Win10, OSX)
My Project - https://github.com/tomboy-notes/tomboy-ng

y.ivanov

  • Full Member
  • ***
  • Posts: 231
Re: Try Finally Except - Help
« Reply #7 on: July 13, 2021, 11:55:40 am »
I am trying to clean up a lot of my code messes.  I am trying to implement error handling where possible and this is my first go at use try...except... statements.  I have used them, typically what I copied from examples on the net.  I really need my procedure to exit or abort if an exception happens.  Simply calling exit; after the On E: EInOutError do gives me a compile error. 

Using AssignFile/RewriteFile/CloseFile raises exceptions with I/O checks enabled, by default they're not:
Quote
Depending on the state of the {$I} switch, a runtime error can be generated if there is an error. In the {$I-} state, use IOResult to check for errors.
I would use streams for the purpose.

 

TinyPortal © 2005-2018