Negative
xs0
xs0 at xs0.com
Mon Feb 27 11:33:10 PST 2006
Andrew Fedoniouk wrote:
> I am with Charles here...
>
> I don't understand why
> on_scope_failure & co. are significantly better than
> try catch finally?
imho, they're not better in all cases, just in some :)
> What is wrong with them?
nothing
> Semantically try-catch-finally are well known and
> widely recognizable constructions.
agreed
> BTW: Am I right in my assumption that
> proposed on_scope_exit / on_scope_success / on_scope_failure
> is a direct equivalent of the following:
>
> try
> {
> [scope code]
> my_on_scope_success().
> }
> catch(...)
> {
> my_on_scope_failure().
> }
> finally {
> my_on_scope_exit().
> }
>
> If yes then again what it wrong with them in principle?
Well, nothing in principle, but like it was said already:
- init and cleanup can be close together (which is good in itself)
- as a consequence, it's harder to forget cleanup
- it's prettier - code that is about linear looks like it's about linear
(see example 2)
- you can reorder them as you see fit (see example 1)
- less typing :)
Example 1:
=======================
on_scope_exit foo();
on_scope_failure bar();
on_scope_exit baz();
// code
=======================
Versus
=======================
try {
try {
try {
// code
} finally {
baz();
}
} catch (Object e) {
bar();
throw e;
}
} finally {
foo();
}
=======================
Example 2:
==================================
// notice how creation and cleanup are together
Logger log = getLogger();
on_scope_exit log.close();
on_scope_failure log.logFailure("...");
File outf = getOutputFile();
on_scope_exit outf.close();
File inf = getInputFile();
on_scope_exit inf.close();
File tmpf = createTempFile();
on_scope_exit tmpf.delete();
on_scope_exit tmpf.close();
// lots of code
==================================
Versus
==================================
Logger log = getLogger();
try {
File outf = getOutputFile();
try {
File inf = getInputFile();
try {
File tmpf = createTempFile();
try {
// lots of code
} finally {
tmpf.close();
tmpf.delete();
}
} finally {
inf.close();
}
} finally {
outf.close();
}
} catch (Object e) {
log.logFailure("...");
throw e;
} finally {
log.close();
}
==================================
More information about the Digitalmars-d
mailing list