Solved

TRY EXCEPT FINALLY

Posted on 2003-10-29
4
416 Views
Last Modified: 2012-03-15
Hi, i was wondering if it is possible to trap an error in a robust way using the
try and except and finally. Before the form shows it will search for a particular
directory such as C:\TEST\BACKUP2 and it is not found on the then it will raise an exception and does not continue the loading of my program. is it possible?
0
Comment
Question by:therooster
  • 2
4 Comments
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 9648265
That is what try except finally is for.

try
  SearchForDirectory();
  Found := True;
except
  Found := False;
end;

Now SearchForDirectoryis either successful and Found := True is executed or the exception hits and
Found := False is executed. The exception is swallowed.

If you want to have the exception raised again then call "raise;" in the except block.
Alternatively use
try
finally
end;
which does not catch the exception, but executes the block of "finally end" no matter if an exception occurs or not.
It also executes even if you call Exit inside "try finally".

If you want both then you have to use two nested try blocks.
try
  try
  except
  end;
finally
end;
0
 
LVL 17

Accepted Solution

by:
geobul earned 250 total points
ID: 9648615
Hi,

You may do that in you project unit this way:

program Project1;

uses
  Forms,
  Unit1 in 'Unit1.pas' {Form1},
  FileCtrl, // DirectoryExists
  Dialogs; // ShowMessage

{$R *.RES}

var d: string;
begin
  d := 'c:\test\backup2';
  if DirectoryExists(d) then begin
    Application.Initialize;
    Application.CreateForm(TForm1, Form1);
    Application.Run;
  end else begin
    ShowMessage('Directory '+d+' does not exist! Terminating ...');
  end;
end.

Regards, Geo
0
 
LVL 17

Expert Comment

by:geobul
ID: 9648654
The same thing could be done in a form's OnCreate event:

uses FileCtrl;

procedure TForm1.FormCreate(Sender: TObject);
var d: string;
begin
  d := 'c:\test\backup2';
  if not DirectoryExists(d) then begin
    ShowMessage('Directory '+d+' does not exist! Terminating ...');
    Application.Terminate;
  end;
end;

Regards, Geo
0
 

Expert Comment

by:_JHL_
ID: 9672918
Hi, the Try Except way it`s a good way to prevent a crash of the programa, or an error that you don't want to raise.
The sintax that I usually do is:
try
  {Here the code that probably raise an exception}
except
 {Here the code alternative if there is an exception OR
exit; {to terminate the process OR}
application.terminate;{to terminate the program}
end;

I hope it helps

JHL
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi TcxGrid group footer summary 3 323
Delphi : could not find program, '...exe' 2 195
select query - oracle 16 100
RESTRequest Parameter 4 43
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question