Delphi Developer Certification Tips #3

Let's share the knowledge with your friends

The tips #3 for the Delphi Developer Certification it’s about exception handling.

Exceptions are exceptional conditions that require special handling; the exception handling provides a standard way of dealing with errors. Using exception handling you will be able to manage errors when it happen and decide what to do.

The RAD Studio documentation has a specific topic about Exception Handling, take sometime and read this topic, it will help you to answer the exam questions related with exceptions.

You may think because you use try..except..end, you know enough to answer the exam questions, I would say NO. Since we have a large pool of questions, the exam will ask you about different aspects of exception handling, like: Re-raising Exceptions, Silent Exceptions and more.

Silent Exceptions is a way to raise a exception without showing the error message, while the regular exception show the error message, but is not only this.

  • Silent exceptions all descend from the standard exception type EAbort.
  • Delphi has a procedure called Abort, which automatically call raise a EAbort exception
When the topic is re-raising exceptions, RAD Studio documentation has a good introduction on that.

When the reserved word raise occurs in an exception block without an object reference following it, it raises whatever exception is handled by the block. This allows an exception handler to respond to an error in a limited way and then re-raise the exception. Re-raising is useful when a procedure or function has to clean up after an exception occurs but cannot fully handle the exception.

For example, the GetFileList function allocates a TStringList object and fills it with file names matching a specified search path:

function GetFileList(const Path: string): TStringList;
var
  I: Integer;
  SearchRec: TSearchRec;
begin
  Result := TStringList.Create;
  try
    try
       I := FindFirst(Path, 0, SearchRec);
       while I = 0 do
       begin
         Result.Add(SearchRec.Name);
         I := FindNext(SearchRec);
       end;
     finally
       FindClose(SearchRec);
     end;
  except
    Result.Free;
    raise;
  end;
end;

GetFileList creates a TStringList object, and then uses the FindFirst and FindNext functions to initialize it. If the initialization fails – for example because the search path is invalid, or because there is not enough memory to fill in the string list – GetFileList needs to dispose of the new string list, since the caller does not yet know of its existence. For this reason, initialization of the string list is performed in a try…except..statement. If an exception occurs, the statement’s exception block disposes of the string list, then re-raises the exception.

The exam goes beyond of the two aspects mentioned here. The following three links will help you to learn more about exception handling:


Let's share the knowledge with your friends
13 replies
  1. J.M. Stoorvogel
    J.M. Stoorvogel says:

    Why not use a single try-finally statement? Inexperienced programmers will undoubtly make the mistake by writing something like try except catch E raise(E)end.

    Reply
  2. J.M. Stoorvogel
    J.M. Stoorvogel says:

    In fact, now you’ve got a resource leak: FindClose is only called in case of an exception, but it should *ALWAYS* be called after FindFirst.

    Correct code should be:
    FindFirst
    try
    {exception possible}
    finally
    FindClose()
    end;

    Reply
  3. J.M. Stoorvogel
    J.M. Stoorvogel says:

    And if you want to free the TStringList, use a
    try
    try
    FindFirst();
    finally
    FindClose();
    end;
    except
    FreeAndNil(result)
    Raise;
    end;

    construction.

    Freeing btw is not the same as making nil. Because of the exception (which indicates that the postconditons cannot be met, by providing a valid TStringList as result), FreeAndNil is not really necessery.

    Reply

Trackbacks & Pingbacks

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.