3 ms·
A much nicer version of this is the Common Lisp condition system. Roughly the way it works is that in addition to being able to raise exceptions, a procedure c
by kmill 5y ago
A much nicer version of this is the Common Lisp condition system. Roughly the way it works is that in addition to being able to raise exceptions, a procedure can ask a condition handler how to proceed. Basically, the condition handlers are dynamically scoped like a try/catch, and the conditions can be finer-grained than a catch-all Error (though to be somewhat fair to Visual Basic, you could also "On Error GoTo handler_label").
Here's how it might look in Python:
try:
f = open("foo.txt")
s = f.read()
condition FileNotExists as c:
handle UseStream(io.StringIO(""))
where when open fails to find a file, Python will look for the first FileNotExists condition handler, pass it a FileNotExists condition object, and then the handler can choose to handle the condition.
On the open side, it could be something like this:
def open(filename):
restartable:
... low-level stuff to open the file ...
if failed:
signal FileNotExists(filename)
...
restart UseStream as r:
return r.stream