[prev in list] [next in list] [prev in thread] [next in thread]
List: kde-devel
Subject: Re: OT: Re: Those dern C++ exceptions
From: Lubos Lunak <l.lunak () sh ! cvut ! cz>
Date: 2001-08-20 9:09:11
[Download RAW message or body]
Dne po 20. srpen 2001 02:19 Malte Starostik napsal(a):
[snip]
>
> 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
> }
> }
void stuff()
{
try
{
some_simple_smart_ptr< Foo > foo = new Foo();
// Do stuff with foo that might throw an exception
}
}
As your punishment, you'll write 100 times 'acquiring resources is
initialization' :)) . Hmm, this reminds me, I wanted to send a mail about
smart pointers to kde-devel few months ago ... maybe I should do so.
Lubos Lunak
--
l.lunak@email.cz l.lunak@kde.org
http://dforce.sh.cvut.cz/~seli
>> 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