Solved

TRY EXCEPT FINALLY

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

762 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