[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-devel
Subject:    Re: OT: Re: Those dern C++ exceptions
From:       Malte Starostik <malte () kde ! org>
Date:       2001-08-20 0:19:45
[Download RAW message or body]

Am Mittwoch, 15. August 2001 23:37 schrieb Richard Dale:
> On Wed, 15 Aug 2001, TAnGorN (Anatoli Gorchetchnikov) wrote:
> > On Wednesday 15 August 2001 10:57 am, Adriaan de Groot wrote:
> > > "Dijkstra's 'goto considered bad' is spot on," so you know that C++
> >
> > AFAIR reading Dijkstra, it was 'goto considered bad and should be
> > replaced wherever possible by while, for etc' which are also hidden
> > gotos... You don't trash 'for' just because it is wrap around 'goto'.
>
> I don't think I've ever needed to use a C goto (very ugly things). And I
> can't say I've used C++ exceptions. But there is more fun to be had with
> the C longjmp() f'n than any other C construct in my opinion. I think
> comparing the goto with C longjmps or C++ exceptions is a bit unfair.
>
> For instance, you can pass a struct to a C function containing a jmp_buf.
> If the function succeeds you long jump to the jmp_buf destination, and if
> the functions fails you just drop through, after undo everything you've
> done, and return. You invert the meaning of 'exception' to make normal
> behaviour the exception. This allows you to implement 'continuation on
> success' as a control strategy - has anyone murdered C++ exceptions in a
> similar manner?
Even more OT, here is something that somehow combines goto-on-error and 
goto-on-success :)
What I really liked about exception handling in Delphi is the try...finally 
construct in addition to try...except:

procedure Stuff;
var
	foo: TFoo;
begin
	foo := TFoo.create;
	try
		// Do stuff with foo that might raise an exception
	finally
		foo.Free;
	end;
end;

which introduces a distinct cleanup-block that is always called, in case of 
an exception or not, something like below in C++ (note, I never used 
exceptions in C++ myself, so maybe even the syntax is wrong :)
try..finally doesn't add actual error handling if used this way, but can ease 
proper cleanup of temporaries alot, even if they are allocated on the heap as 
there is a central place where to put all those boring delete()s :)
If an exception is raised (thrown) during execution of the try..finally 
block, the finally...end block is executed and then the exception is raised 
as normal, if no exception is raised at all, still the complete 
try...finally...end block is executed.

void stuff()
{
	Foo *foo = new Foo();
	try
	{
		// Do stuff with foo that might throw an exception
		delete foo;
	}
	catch (...)
	{
		delete foo;
		throw(); // Not sure how this is done in C++, meant: re-throw the caught 
exception
	}
}

-- 
Malte Starostik
PGP: 1024D/D2F3C787 [C138 2121 FAF3 410A 1C2A  27CD 5431 7745 D2F3 C787]
 
>> Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to unsubscribe <<

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic