lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> And IMHO here's the point: you have to consider every possible exception
> the same as the error return codes of ie a C function.

As Michael Richter says, this is not true: the point of exceptions is
precisely that you *don't* have to handle them when the function that could
raise them returns (and where you probably can't handle them sensibly in any
case).

If you want to make sure that you've handled all exceptions somewhere (I
didn't say "correctly"!), you can use type-checking: in Modula-3 the
exceptions that a procedure can raise are given as part of its signature;
you can then check that somewhere lexically above each call, there is a
handler for each exception. A crude check, and not possible in dynamically
scoped languages such as Lua, I know.

More practically, the point is this: you don't end up wrapping a catch
around every function call, because you only have to handle exceptions that
affect resources that the call site "owns". In C, you have to check *every*
return code and handle it in some way, even if you can't do anything about
it in the current function. With exceptions, you effectively need a
"finally" clause for resources that are used by the current function.

Another way to look at it is to decouple "finally" from "catch": every
function should finalize all the resources it uses. Then, you don't have to
remember to catch exceptions; instead, the focus is on making sure you
finalise resources.

You could have the following syntax for blocks:

do <block> [finally <block>] end

Every block containing "finally" is effectively an exception handler, whose
finally clause is always run. If you want to, you can check for specific
exceptions in the finally clause; otherwise, you can just use it to finalise
resources that need to be released, safe in the knowledge that the code will
run even if there's an exception in the main block. But rather than having
to check what exceptions each function called in the block may raise
(something a lazy programmer may not want to do), you can ask the more local
question "what resources need finalising?".

This is really equivalent to having an exception handler for exceptions you
may need to handle, rather than for those that may be raised.

I think I've repeated myself a bit here, but I hope I've been clear in the
process.

-- 
http://sc3d.org/rrt/
L'art des vers est de transformer en beautés les faiblesses (Aragon)