Re: Why finally?

Tech-Archive recommends: Speed Up your PC by fixing your registry




"Jon Davis" <jon@xxxxxxxxxxxxxxxxxxxxxxxxxxxxx> skrev i melding
news:uuTSmZFYGHA.3972@xxxxxxxxxxxxxxxxxxxxxxx
I understand that the finally sub-block will execute regardless of whether
try succeeded or not. But so will code that follows try...catch. So then
what is the difference between ...

try {
do.something.that.breaks();
} catch {
do.something.that.works();
}
cleanup();


.. and ..

try {
do.something.that.breaks();
} catch {
do.something.that.works();
} finally {
cleanup();
}

.. other than organizational aesthetics ??


First of all, its not very common, or shouldn't be common to use catch all
statements.

The sole purpose of finally is to ensure that cleanup code is executed when
an exception is raised. Because of this it is also good code practise, c#
coders know that any cleanup will be within the finally block.

Using a finally block will also do a much better job than relying on catch
all statements and a subsequent line of code that in most cases will be
fired, but not all.
For example, consider the following cases:
- An exception is raised in the catch block, finally will be executed, your
first example will not.
- Some time in the future, the code is modified so that the catch statement
only catches specific exceptions, finally will be executed, your first
example will not.
- Some time in the future, your catch statement includes a return statement,
finally will be executed, your first example will not.
- Im sure there is more..

So no, its not just aesthetics, finally is for cleanup, your example just
happen to work in your idea of how the intended code will be executed, and
assumptions such as yours will cause you pain in the end.

- Magnus


.



Relevant Pages

  • Re: Character advancement in games (was Re: Alien RPG cancelled, Obsidian in trouble)
    ... exception of Briarroot, whose smug selfcentredness makes me want to kill him, ... might be able to, if we're not too busy handing billions to GM or some other corporate fuckwits who have paid themselves huge bonuses while screwing their employees, customers and creditors ... ... or you don't, and you think that there is such a thing as the common good, and that it's worth pursuing per se, and not just as a by-product of self-interest. ... Briarroot does. ...
    (comp.sys.ibm.pc.games.rpg)
  • Re: Returning external resources and Exceptions
    ... > when an exception is thrown. ... there remains the problem of code duplication. ... The same is true for most other cleanup methods in the API: ...
    (comp.lang.java.help)
  • Re: Operation can be dispatching in only one type
    ... exception), as Program_Error always represents a program bug. ... cleanup) usually represents a bad design. ... use anonymous access types. ... whether a check will fail is known statically for any compiler ...
    (comp.lang.ada)
  • Re: Wow! Its a Yogh!
    ... "swamp" would then be an exception. ... I have never encountered before now any such claim like this about the sound "blocking" the sound change of a short-o that is common in "wa-" words. ...
    (sci.lang)
  • Re: Find rare bugs - SEH and c++ exception handling
    ... Such pain is more common than any of us would like to admit. ... cause is often a bad pointer or code that cleans up an array ... When the kernel detects an application exception, ... The bug is not reproducible and occours without user interaction (there ...
    (microsoft.public.pocketpc.developer)