therooster
asked on
TRY EXCEPT FINALLY
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?
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
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
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
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;