Beyond Exception Handling: Conditions and Restarts
gigamonkeys.com
gigamonkeys.com
The compiled CL code knows about the convention and follows it, but other JavaScript code doesn't and isn't affected by it.
That said, it's a wonderful model to know about/follow if you come into the rare case that makes reimplementing it where you can useful.
def parse_log_file(f):
result = []
for line in f.readlines(): # Fails for large files
parsed_line = None
retries = 0
while retries < 5:
try:
result.append(parse_log_entry(line))
break
except MalformedLogEntry:
line = fix(line)
retries += 1
return resultThe calling code can choose the appropriate restarts base on information that the callee code does not - and should not - have access to.
In the common case, is that not equivalent to the caller catching specific errors by wrapping the callee in a try/except? Could you elaborate?
Conditions/restarts make a separation of concerns, particularly when the caller knows which of the restart is the more appropriate, if the callee has not enough information/context to choose, and if the callee has more information on how to solve the problem.
I guess the separation can be summarized by: difference between knowing WHAT and HOW to do something. A useful abstraction I guess.
except MalformedLogEntry, ex:
line = fix(line, ex)
and having ex encode the information that fix() needs to do its job?If that's the case, then I can see the value in having the ability to pass such information around baked into the language. The code I have is growing more and more ugly, and the only way to simplify it is to introduce some kind of a coding convention that is not common to Python.
I don't know how to solve that in a language without restarts. The information of what to do could well be asked to the final user (with a dialog box) once an error is catched, the callee would then restart at the point where the execution was interrupted applying the chosen strategy.
EDIT: rereading your Python code, I guess you're right and have the closest equivalent.
This, by no coincidence I'm sure, reminds me a lot of how signals are handled in UNIX/POSIX. I guess you could simulate this very crudely with those by sending yourself a SIGUSR[1|2] :).