Hello I have this problem:
I have 2 methods (in reality it is much more, and the need comes from working with database try catches, if you like I can show the original stuff):
procedure MethodA(2 parameters);
begin
// body of A using the 2 params
end;
procedure MethodB(10 parameters);
begin
// body of B using the 10 params
end;
These methods can raise exceptions. And the management of these exceptions is always the same.
Therefore I decide to wrap them in a ExcHandlingWrapped version:
procedure ExcHandlingMethodA(N extra parameters necessary for the external part + 2 parameters from the real MethodA);
begin
// do things before (always the same regardless of MethodA) using the N params
MethodA(his 2 parameters);
// do things after and manage exceptions (always the same regardless of MethodA) using the N params
end;
procedure ExcHandlingMethodB(N extra parameters necessary for the external part + 10 parameters from the real MethodB);
begin
// do things before (always the same regardless of MethodB) using the N params
MethodB(his 10 parameters);
// do things after and manage exceptions (always the same regardless of MethodB) using the N params
end;
I was wondering if is possible directly in the language or someone can suggest me a trick to do something:
procedure ExcHandlingUniversal(N extra parameters necessary for the external part, parameterless proc);
begin
// do things before (always the same regardless of proc) using the N params
proc();
// do things after and manage exceptions (always the same regardless of proc) using the N params
end;
so that I can do something like:
ExcHandlingUniversal(N parameters, () ->
{
MethodA(his 2 parameters);
});
ExcHandlingUniversal(N parameters, () ->
{
MethodB(his 10 parameters);
});
Sorry for the syntax is somehow mixed with C#, but I hope you understand what I mean.
If I think to pascal I should do something like
var
MethodAP1: type;
MethodAP2: type;
procedure MethodACaller();
begin
MethodA(MethodAP1, MethodAP2);
end;
var
MethodBP1: type;
MethodBP2: type;
// up to 10
procedure MethodBCaller();
begin
MethodB(MethodBP1, MethodBP2, up to 10);
end;
and then do something like
MethodAP1 := value;
MethodAP2 := value;
ExcHandlingUniversal(N parameters, @MethodACaller);
MethodBP1 := value;
MethodBP2 := value;
// up to 10
ExcHandlingUniversal(N parameters, @MethodBCaller);
Do I don't like so much because it might open problems in case of concurrent threads due to the external variables. And is kinda ugly.
Some idea?
Thank you