Solved

TRY EXCEPT FINALLY

Posted on 2003-10-29
4
409 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now