Using Try and Finally to Help Prevent Memory Leaks During Dynamic Object Creation in Delphi - No Except Statement?
(Page 4 of 4 )
Now I have been asked before how Delphi programmers use Try/Finally without using the "Except" statement. What is the purpose of using "Try" when you are not trapping exceptions ?
Try/Finally statements are used to make sure that the resources allocated during the creation of the object are freed after they have served their purpose. While try statements may be used to trap exceptions, in these examples their purpose is not exception trapping. Here is what the Delphi 7 manual says:
"While it is possible to raise exceptions for almost any reason, and to protect almost any block of code by wrapping it in a try...except or try...finally statement, in practice these tools are best reserved for special situations."
Delphi has a default exception handler to trap errors, so it is not necessary to catch every exception by creating a handler. Any exception not specifically handled is ultimately caught and handled by the HandleException method of the global Application object. Besides, in situations where the try block has multiple statements performing diverse tasks, adding an Except block would be overkill and may sometimes end up displaying two error messages to the user: the error message that we create and the error message inserted by Delphi for one of the statements that failed during execution. Delphi's help file says that we shouldn't try handling exceptions we don't expect and don't know how to handle.
But I don't intend to say that there is lesser scope or need of exception handling in Delphi. On the contrary, Delphi not only allows the developer to catch runtime errors and handle them in any manner, it also provides the Exception class (declared in sysutils) which can be instantiated with a raise statement to generate an exception event to perform certain tasks. In fact, it would be prudent to handle exception in a "try... except" statement to perform alternative action on expected errors.
A nice way to handle exceptions, especially in database applications, would be through the object events like OnPostError and OnDeleteError. You can also create a global exception handler by coding the OnException event of the Application object. This event can handle any error raised during the execution of the code. I think its a good idea to handle exceptions in one block instead of coding them throughout the program.
Then there are situations like validation of fields which need to be handled in the same block. Conditional statements like "if..else" statement are perhaps the simplest way to prevent/handle these errors
if Edit1.Text:='' then
begin
ShowMessage('Please enter some text');
Exit;
end
else
So it is not necessary that every "try" block should be followed by an "except" block before a "finally" block is called except when you want to trap expected errors and/or perform an alternative action. Using "try.. finally" after object creation ensures that the allocated memory is freed regardless of whether the statements in the "try" block raised an error or not. I think its a very stoic way of handling errors (if anyone is interested in stoicism do visit my blog).
So if you are coding operations like calling constructors which allocate memory, it would be advisable to put it before the "try" block and "free" the resource in the "finally" block to avoid those bothersome memory leaks.
Danish Ahmed
Mindfire Solutions
http://www.mindfiresolutions.com
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |