I'm in the middle of reading The Pragmatic Programmer by Andrew Hunt
and Dave Thomas
(http://www.pragmaticprogrammer.com/ppbook/index.shtml) and it talks
about the finally block of the exception handling of some languages
like ActionScript has and how it's useful in helping apply a number of
principles described in the book. As a result I finally understand how
to use the finally block.

The book talks about 2 principles:-

a. Finish what you started.
b. Don't Repeat Yourself (DRY).

The DRY principle is about not writing the same bit of code more than
once because things always change and it's human nature to
forget/overlook all occurrences of some repeated bit code that needs
to change. Hence bugs are introduced and maintenance increases and
time to enhance existing code increases.

Finish what you started is about resource management. The routine that
opens/allocates/creates some resource should also
destroy/release/close that resource. That way you don't run out of
resources because it's opened in one place and closed in another or
more places. Running out of resources is a bug that could of been
avoided by managing that resource in one place.

So how does the finally block help? The finally block is guaranteed to
always be executed regardless of any error being caught or not. It's
also guaranteed to be executed if the catch block exits the routine.
So how does it help?

If you created, for example, a movieclip and an error occurs, the
principle of "finished what you started" says you need to destroy the
movieclip in the routine that created it. Without the finally block,
you would violate the DRY principle by destroying the movieclip in the
catch block as well as at the end of the routine. By putting in a
finally block, you only do that code once.


Chris
--
Chris Velevitch
Manager - Sydney Flash Platform Developers Group
www.flashdev.org.au
_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to