Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2013-12

how to simplify try try except end finally end?

9 messages · 6 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Dennis Poon

Is there a way to further simplify the following?

I need to make the exception message more indicative of the context of the error and I hope to combine the 2
try blocks into one.

It seems possible in Java and C# but not in FPC.
Is there a way to combine them like

   try
   except
   finally
   end;

Dennis

------------
Obj := TSomeObject.Create;
TheContext := 'after object creation';
try
   try
     TheContext := 'Before doing task 1';
      //task 1
    TheContext := 'Before doing task 2';
    //task 2
    TheContext := 'Before doing task 3';
   //task 3
   finally
     FreeAndNil(Obj);
   end;
except
   On E : Exception do begin
      E.Message := TheContext+E.Message ;
     raise;
   end;
end;
-------------
Reinier Olislagers
to Dennis Poon
Dennis Poon wrote
It seems possible in Java and C# but not in FPC. Is there a way to combine them like

No.
Search the archives for proposals for syntax like this that didn't lead to anything.

Lukasz Sokol
to Reinier Olislagers
Reinier Olislagers wrote 9 lines
On 26/12/2013 13:09, Dennis Poon wrote: > It seems possible in Java and C# but not in FPC. > Is there a way to combine them like No. Search the archives for proposals for syntax like this that didn't lead to anything.

It did lead exactly to Sven liking it ;) and (I think) to submitting a feat request.

IOW, I suppose patches are welcome (but I'm not competent enough to do that)

-L.

Reinier Olislagers
to Lukasz Sokol
Lukasz Sokol wrote 7 lines
On 26/12/13 12:13, Reinier Olislagers wrote: > On 26/12/2013 13:09, Dennis Poon wrote: > Search the archives for proposals for syntax like this that didn't lead > to anything. > > It did lead exactly to Sven

Ah yes, you're right that *is* progress :)

PS: I like it, too - used to it in .Net, but not too bothered by having to work around it.

Mark Morgan Lloyd
to Reinier Olislagers
Reinier Olislagers wrote 13 lines
On 27/12/2013 10:17, Lukasz Sokol wrote: > On 26/12/13 12:13, Reinier Olislagers wrote: >> On 26/12/2013 13:09, Dennis Poon wrote: >> Search the archives for proposals for syntax like this that didn't lead >> to anything. >> >> > It did lead exactly to Sven Ah yes, you're right that *is* progress :) PS: I like it, too - used to it in .Net, but not too bothered by having to work around it.

For what it's worth, I'm ambivalent. Not because I feel that try stacks couldn't or shouldn't be simplified, but because I feel that whoever overloaded try for two different structures should be shot.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]
Jürgen Hestermann
to Mark Morgan Lloyd
Mark Morgan Lloyd wrote
but because I feel that whoever overloaded try for two different structures should be shot.

Yes, that's what I thought too as I discovered this long ago. I think this is yet another cuckoo's egg from Borland/Embacadero...

Mark Morgan Lloyd
to Dennis Poon
Dennis Poon wrote 6 lines
> For what it's worth, I'm ambivalent. Not because I feel that try > stacks couldn't or shouldn't be simplified, but because I feel that > whoever overloaded try for two different structures should be shot. I don't know enough about compiler. Could you kindly elaborate on the above please?

There are two distinct programming structures, with- as I understand it- significantly different code generation requirements. Both are introduced by the reserved word "try".

The first structure, which is broadly comparable with most modern languages, is

try
..
raise...
..
except
..
end;

with "raise" transferring control to the exception handler.

The second structure is

try
..
finally
..
end;

and I might be wrong but I don't think there's an analogue of "raise" in this case.

In my own scripting stuff I've used

start
..
stop...
..
finally
..
end;

This obviously doesn't reduce verbosity, but my own opinion is that it improves clarity since you won't in general encounter

try
   try
..

and then have to go to the end of the blocks to work out what's intended.

In principle it would be possible to define "start" as a macro, but I don't know how badly that would confuse Lazarus/Synedit highlighting and code folding. In practice it's completely incompatible with Delphi (and, as far as I know, every other Pascal variant) so is probably not a habit to get into.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]
Sven Barth
to Mark Morgan Lloyd: For what it's worth, I'm ambivalent. Not because I feel that try stacks couldn't or…

Am 27.12.2013 11:22 schrieb "Mark Morgan Lloyd" <

Show quoted text 23 lines
: Reinier Olislagers wrote: > > On 27/12/2013 10:17, Lukasz Sokol wrote: >> >> On 26/12/13 12:13, Reinier Olislagers wrote: >>> >>> On 26/12/2013 13:09, Dennis Poon wrote: >>> Search the archives for proposals for syntax like this that didn't lead >>> to anything. >>> >>> >> It did lead exactly to Sven > > > Ah yes, you're right that *is* progress :) > > PS: I like it, too - used to it in .Net, but not too bothered by having > to work around it. For what it's worth, I'm ambivalent. Not because I feel that try stacks

couldn't or shouldn't be simplified, but because I feel that whoever overloaded try for two different structures should be shot.

AFAIK most other languages that support a finally also use try for both. So I would say that Borland just went with the flow...

Regards,
Sven