Post by chris at seberino.org ()With exceptions you can basically say "I don't care what goes wrong....Just
keep retrying until successful." That little try block will catch EVERYTHING!
That is handly.
Yes, exactly. I've worked with libraries where *opening* the socket throws
an exception if it can't connect, tho. That's just silly.
Post by chris at seberino.org ()Post by Darren New=> one try/catch at the top level to log unexpected errors,
I don't understand this. Are you you saying your ENTIRE program is inside one
big try block?
For server programs? Sure. Some unexpected error happens that you haven't
already caught, you catch the exception, log the error, and then either bail
out or clean everything up, depending.
If it's a video game, say, you put the main game loop inside an exception
catch, then display the traceback when it fails, with the hope that it'll
help you debug during beta test.
Post by chris at seberino.org ()Post by Darren New=> one try/catch around (say) each transaction (which is what you seem to
be describing),
If you haven't enough of these then your top level try block is superfluous
right?
Not always, but sure, it ought to be. Depends how big your transactions are,
and how much you do during initialization, termination, and between
transactions. Not everything happens inside a 'transaction' either.
But mainly it's for logging purposes and such.
Post by chris at seberino.org ()Post by Darren NewPeople who use exceptions instead of error return codes are the ones who
complain that exceptions are uglier. People who use them the same way
you'd use setjump/longjump are the ones who understand the stuff.
I'm not familiar with setjump/longjmp so I missed that point.
setjump is a label, longjump is a goto, basically, except it can "goto"
anywhere in the code.
Post by chris at seberino.org ()Post by Darren NewI bring this up, because one of his decisions is that you can't catch an
exception and pick up from where you left off - that ability indicates
bad design, in the same way that resuming a database transaction that
failed without first rolling back the changes is a bad idea.
So he's saying you HAVE to restore state to before the exception?
In this case, each function has an optional "rescue" block. If there's no
rescue block, the exception propagates. If the "rescue" block doesn't end in
"retry" then the rescue block runs and then propagates the exception
(basically, catch followed by rethrow, or finally if you would). If the
block ends in "retry", then it basically branches back to the start of the
function. There's no way to execute the line *after* the line that caused
the exception without starting the whole function over from scratch.
So, yes, exactly your example, with retry. Without the retry, you just get
to fix your invariants before returning.
Post by chris at seberino.org ()I'm not sure how he'd enforce that.
Well, that's what the rest of the language is about. It's all kind of
interrelated. If you don't actually put in code what "restore state" means,
then there's no way to enforce it. But he does have code structures that
indicate what it means to "restore state", so that's OK.
Post by chris at seberino.org ()Would he be ok with my socket example above? There you
are retrying the socket connection so I suppose that counts as restoring back
to your original state no?
It's a whole tome of a book. It's available online free from some places,
altho I can't say for sure how it got there. (Actually, IIRC, when you buy
the development environment it comes with a PDF copy of the book, so it's
probably at least semi copyright violation.) But the actual text is
worthwhile, even if you never use the language, just to see how you can sit
down and logically design something like that.
But to answer the question, you have to throw an exception if you can't meet
your postconditions, and even if you throw an exception you still have to
fix your invariants. Look up "design by contract" for an idea of what all
that means.
--
Darren New, San Diego CA, USA (PST)
Quoth the raven:
Need S'Mores!