Solved

TRY EXCEPT FINALLY

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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 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