Solved

try and finally

Posted on 2001-06-11
7
162 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Suggested Solutions

Title # Comments Views Activity
Performance of SQL statement 37 137
Delphi and Access based Enumeration 9 77
Sending files from  idTcpServer Socket to idTcpClient 2 52
Broadcast a message using ICS 2 20
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

740 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