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

Adding try..finally around Enter/LeaveMethod (Delphi)


Good day,

Is there a way to force adding of try…finally around Enter/LeaveMethod calls?

If exit clause was used inside the code it will screw up with log indents.

Thank you,


Hello Alex,

there’s currently no support for this in the plugin. That’s a popular feature request (together with support for IFDEFs) and already on our feature request list, I just added another vote. I can’t say when/if this feature will be available but we currently plan to publish the plugin code on so that users can make their own changes.




Thank you for fast response.
I really hope you guys can add this feature as it would be real time-saver for many.



I also would like to be able to automatically add try/finally clauses into the enter/exit declarations, before I consider purchasing this product.

What is the status of being able to get the plugin source to be able to customise it for our requirements?




Hello Craig,

We do not have an update regarding the plugin source unfortunately. You could also consider using the TrackMethod function which mimics the behavior of a full try/finally enter/leave block (TrackMethod is not supported by the IDE plugin though).

I hope this helps,


Hello Tobias,

I would like to have a kind of try…finally…end block too. At the moment I have to extend each line manually. My code looks like this:

procedure InternalAddFiles; begin {$IFDEF SMARTINSPECT}SiMain.EnterMethod(Self, 'InternalAddFiles'); try{$ENDIF} ... {$IFDEF SMARTINSPECT}SiMain.LogInteger('anyvalue', 100);{$ENDIF} ... {$IFDEF SMARTINSPECT}finally SiMain.LeaveMethod(Self, 'InternalAddFiles'); end;{$ENDIF} end;
My earier logging sessions with SmartInspect took lots of time, because my logged methods seemed to be recursively called …

Please add the possibility to wrap some “individual” texts around the SiMain.EnterMethod and SiMain.LeaveMethod statements. Personally I always add ifdef statements so I can leave some logs and activate them when I need them.

Best regards,


Hello Michael,

Thanks for your suggestion. IFDEFs or a general text before/after the statements is already on our feature request list. That said, it may make sense to automate this yourself with a “smart” search & replace (e.g. based on regular expressions). The search & replace would basically look for lines with SiMain.EnterMethod, SiMain.* and SiMain.LeaveMethod and then just wrap these lines with the desired IFDEFs and try/finally blocks, so you don’t have to add this code manually.

I hope this helps,


[quote=Alex Poloziouk]Good day,
Is there a way to force adding of try…finally around Enter/LeaveMethod calls?
If exit clause was used inside the code it will screw up with log indents.


when you will work off the feature request list?
The insertion of “try finally end” should not take much time … :confused:

It would be great of course, if you add two memos in the Smartinstect configuration, in which everyone can adapt their ows ENTER and LEAVE calls for "INSTRUMENT … ".
Two placeholders for the variables SiSession and unit name and this is done…

You want that we spend money for the maintenance for you.
Please do something for this.

The “EXIT problem” is really annoying…



Thanks for your feedback. Yes, I understand that this would be useful to have and we will look into this. I don’t have a time frame for this at the moment though unfortunately. While adding such a feature looks fairly simple, it’s actually not that straightforward especially as we would also need to implement a way to uninstrument the added code, change the indentation of the existing code and of course make this work with all possible source code formatting and language constructs.

The complexity of this is increased by the fact that there’s no complete and official Delphi language grammar. We do plan to look into this though and will likely concentrate on supporting the TrackMethod call instead (which has the same behavior as using try…finally).




The indentation is a negligible feature. The most important request is to insert a try … finally block and optionally a conditional define.

How will TrackMethod work? Do I have to “workaround” each call to this method?



Hello Michael,

sorry for the late response, I didn’t receive an email update about the posting. The TrackMethod call basically simulates the behavior of an EnterMethod, try…finally, LeaveMethod call sequence. This is implemented by returning an interfaced object, which is automatically free’d by Delphi when it goes out of scope. So instead of calling Enter/LeaveMethod you would just call TrackMethod once at the beginning of a method.



Sounds good. When will it be available? :wink:
Please consider the conditional defines when implementing the TrackMethod mechanism.



Hi Michael,

I don’t have a time frame for this unfortunately. You can already use the TrackMethod method, but it’s not clear when/if we will have this as part of the Delphi IDE plugin. We do have this on our list as well as the conditional defines and we want to look into this in the future.




I had to dig a very old project recently to change a couple of sequences.
Hundreds of units, tens of thousand methods, numerous EXIT’s.

SmartInspect was a great help for me to think me inside the old code.

But, I have wasted a whole 10 hours to instrument the units, to rename EnterMethod into TrackMethod per search & replace and delete all LeaveMethod calls. That was not nice job. :rolleyes:

Can leave the complicated part with the “conditional defines” on your long-term roadmap, but continue for it relatively promptly to provide the alternative automatted instrumentation via TrackMethod?
Really, that would be a huge help.

I think it is not so much do for you …

// PutText ( 'EnterMethod('+aMathodName+');' ) // old
PutText ( 'TrackMethod( '+aMathodName+');' ) // new instrumentation behavior
// FindMethodEnd; // old
// PutText ( 'LeaveMethod('+aMathodName+');' ) // old



Hello Jens,

Thanks for your feedback. It’s still on our TODO list but it’s not yet clear if/when this will be added. It’s a bit more complicated than changing just a few lines of code unfortunately (e.g., this must also be supported when uninstrumenting the code and also affects the parsing code etc.), but I understand that this would be useful to have, especially for large projects of course. That said, I don’t have a time frame for this enhancement currently I’m afraid.