Join 34,000+ subscribers and receive articles from our blog about software quality, testing, QA and security.
 

Enter/Leave vs. TrackMethod


#1

Hi there,

Is there any real difference between using the TrackMethod or the Enter/Leave procedures? The TrackMethod seems easier to use, seeing that you don’t need to encapsulate it in a try/finally construction. Any thoughts?

Regards,
Gerben ten Wolde


#2

Hi Gerben,

You are right that there’s no real difference between using EnterMethod/LeaveMethod and TrackMethod. The only difference is that EnterMethod/LeaveMethod can be used without try/finally and it’s therefore sometimes easier to see where exceptions occurred (TrackMethod is using a try/finally block implicitly). Additionally, EnterMethod/LeaveMethod is included in the Delphi library for compatibility to the other libraries.


#3

Dennis,

You answered: “The only difference is that EnterMethod/LeaveMethod can be used without try/finally and it’s therefore sometimes easier to see where exceptions occurred”

Are you sure you don’t mean ‘TrackMethod’ in stead of ‘EnterMethod/LeaveMethod’? If not I do not understand your reply.

Yours,
Gerben


#4

Hi Gerben,

EnterMethod/LeaveMethod is correct in this context. What Dennis meant is that TrackMethod always acts like a combination of EnterMethod/LeaveMethod with a try/finally block. Since this might not be the desired behavior in some cases, TrackMethod might not always be applicable. I think this is best explained with an example. Imagine the following code snippet:

procedure TForm1.Button1Click(Sender: TObject);
begin
SiMain.TrackMethod(Self, ‘Button1Click’);

// …
end;

The way the TrackMethod method works, you are always using a try/finally block implicitly/automatically. The code snippet above is therefore equivalent to the following lines:

procedure TForm1.Button1Click(Sender: TObject);
begin
SiMain.EnterMethod(Self, ‘Button1Click’);
try
// …
finally
SiMain.LeaveMethod(Self, ‘Button1Click’);
end;
end;

As I mentioned, this might not always be the desired behavior. It is sometimes advisable to use EnterMethod/LeaveMethod without a try/finally block. Imagine an exception in the body of the try/finally block. When you are using EnterMethod/LeaveMethod with a try/finally block, then the code in the finally section is nonetheless called and it can be difficult to find the exact location in the log where the exception occurred. This is not the case if you omit the try/finally block like in the following examples:

procedure TForm1.Button1Click(Sender: TObject);
begin
SiMain.EnterMethod(Self, ‘Button1Click’);

// …

SiMain.LeaveMethod(Self, ‘Button1Click’);
end;

When using EnterMethod/LeaveMethod this way, the call to LeaveMethod will not be executed when an exception occurs between EnterMethod and LeaveMethod. This way it is usually easier to tell in which method the exception occurred.

I hope I could answer your question to your satisfaction.


#5

Indeed you have, thanks.