Solved

try and finally

Posted on 2001-06-11
7
165 Views
Last Modified: 2010-04-06
what is the importance of this instruction try and finally
please give an example in which this instructions are best suited for...
0
Comment
Question by:aerokevs
[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
7 Comments
 
LVL 22

Accepted Solution

by:
Mohammed Nasman earned 10 total points
ID: 6176259
Hello

  if you want to execute some code, like create a button in run time, and u want to free that after u finish the operation if the code worked fine or you if got error the try clause will execute the finally part, you can find more info in the help.

from delphi help:

Try...finally statements

Sometimes you want to ensure that specific parts of an operation are completed, whether or not the operation is interrupted by an exception. For example, when a routine acquires control of a resource, it is often important that the resource be released, regardless of whether the routine terminates normally. In these situations, you can use a try...finally statement.
The following example shows how code that opens and processes a file can ensure that the file is ultimately closed, even if an error occurs during execution.

Reset(F);

try
 ...  // process file F
finally
  CloseFile(F);
end;

The syntax of a try...finally statement is

try statementList1 finally statementList2 end

where each statementList is a sequence of statements delimited by semicolons. The try...finally statement executes the statements in statementList1 (the try clause). If statementList1 finishes without raising exceptions, statementList2 (the finally clause) is executed. If an exception is raised during execution of statementList1, control is transferred to statementList2; once statementList2 finishes executing, the exception is re-raised. If a call to the Exit, Break, or Continue procedure causes control to leave statementList1, statementList2 is automatically executed. Thus the finally clause is always executed, regardless of how the try clause terminates.

If an exception is raised but not handled in the finally clause, that exception is propagated out of the try...finally statement, and any exception already raised in the try clause is lost. The finally clause should therefore handle all locally raised exceptions, so as not to disturb propagation of other exceptions.


0
 

Expert Comment

by:mike_tipping
ID: 6176981
Or in other words...

If you wrap a:

Try
  ...
Finally
  ...
end; //finally

Around something, the thing you put in the fially will be executed no matter what happens between the try and the finally. As Mnasman says, an obviouse example of this is if you create a memory object so:

myTList := Tlist.Create;
Try
  //Do somthign with  myTList
Finally
  if Assigned(myTList) then
    myTList.Free;
end; //Finally

So the myTlist is always free'd even if the code crashes during the 'Do somthign with  myTList' bit.

   
0
 
LVL 8

Expert Comment

by:TOndrej
ID: 6178082
Hi mike_tipping,

myTList := TList.Create;
try
 //Do somthign with  myTList
finally
  if Assigned(myTList) then // this is unnecessary - Free checks for nil
    myTList.Free; // besides, myTList can never be nil here,
    // unless it's assigned value of nil somewhere in "Do somthign" part
end;
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 27

Expert Comment

by:kretzschmar
ID: 6180282
try
  to get an answer
finally
  grade the expert
end;

:-))
0
 
LVL 4

Expert Comment

by:StevenB
ID: 6184120
 ... of course really safe coders do something like this ...

myTList := TList.Create;
try
  //Do somthign with myTList
finally
  myTList.Free;
  myTList := Nil;
end;

  ... or better still ...

myTList := TList.Create;
try
  //Do somthign with myTList
finally
  FreeAndNil(myTList);
end;

  This ensures that you don't inadvertently access violate when you accidentally try and access myTList after it's been freed.



  aerokevs, the message is that you should use try .. finally EVERY time you create an object at run time which needs cleaning up, always protect your resources like this. If you have multiple objects it's safest to do something like this:


Object1 := TObject1.Create;
try
  Object2 := TObject2.Create;
  try
    // Do something with your objects
  finally
    FreeAndNil(Object2);
  end;
finally
  FreeAndNil(Object1);
end;

  The alternative:

Object1 := TObject1.Create;
Object2 := TObject2.Create;
try
  // Do something with your objects
finally
  FreeAndNil(Object2);
  FreeAndNil(Object1);
end;

  could potentially lead to problems if there is an exception raised during either
Object2 := TObject2.Create;
or
FreeAndNil(Object2);

  the lesson is to start the try block immediately after the create and to free the object immediately after the finally statement.
 
0
 

Author Comment

by:aerokevs
ID: 6189695
can try and finally be useful in databases too..
0
 
LVL 4

Expert Comment

by:StevenB
ID: 6189720
 It can be useful anywhere you want to ensure that a certain piece of code is executed, whether an exception is thrown or not. In the case of databases it would be good practice to use it to ensure that your queries and tables are closed, to prevent your application locking out a table.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses
Course of the Month6 days, 5 hours left to enroll

626 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