Solved

TRY EXCEPT FINALLY

Posted on 2003-10-29
4
420 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

751 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