"David Douthitt" <DDouthitt / cuna.com> writes:

> I'm rather confused by Exception Handling.  The descriptions in the
> FAQ and the Ruby Manual about begin ... rescue ... ensure ... else
> .... end are not very clear.  Here are some questions I might ask to
> either author(s):
> 
> 1. What is a good use of this construct?

It helps to catch exceptions that would otherwise terminate your
program.

> 
> 2. What if no rescue clause is matched?

The exception is propagated back up the stack

> 3. When is the ensure clause run?  After every exception?  Only when
>    the "normal flow" is complete?  Both?

Both -- basically it is guaranteed to be run regardless of how the
block exits.

> 4. Why should this be used instead of catch and throw?  When is it
>    better to use one over the other?

Exceptions have more flexible matching semantics - you can match on
multiple exceptions, and match on parent classes. Also, there's the
practical reason: the library code raises exceptions on error.

> 5. I assume that "retry" restarts the command that failed, not the
>    command after it.  Is this true?  Some languages would use the
>    command after the failure.

Retry retries the _entire_ begin/end block. Take it again, Joey, from
the top.

> 6. How does the use of "retry" affect the "rescue" clause?  The
>    ensure clause?

Not all all. It basically is a 'goto' back to the top. Any exceptions
that fire the next time around will be handled normally.

For an example of retry in action, se ruby-talk:[03217].

> 1. Raising an exception in "at_exit" code block causes the rest of
>    the code to be quietly ignored (no exception raised?)

Because you haven't established an exception handler, so it exits to
the top level (just as if you had an unhandled exception anywhere).

> What special concerns are there for exception handling in at_exit
> code?  What about exceptions raised in "exception handling" cases
> such as rescue clauses?

I'm going to stick my neck out here. I'd say that having exception
handling tends to mean you don't need at_exit code as much as you
might do in more primitive languages. Instead, you handle problems as
they arise. If noting else, you can put your main program in a
begin/end block and handle any residual exceptions it its rescue
clause.

> Should be a whole book (or chapter at least) on exception handling

Chapter 8 good enough?   ;-)



Dave