Commit a7de7041 authored by Kevin Modzelewski's avatar Kevin Modzelewski

More exceptions experiments

parent 153ac11a
...@@ -10,3 +10,32 @@ def throw(x): ...@@ -10,3 +10,32 @@ def throw(x):
# Both print "Exception" # Both print "Exception"
throw(Exception) throw(Exception)
throw(Exception()) throw(Exception())
def catches(t, e):
try:
raise e
except t:
return True
except:
return False
print catches(Exception, Exception)
print catches(Exception, Exception())
print catches(Exception(), Exception())
print catches(Exception, StopIteration())
print catches(Exception, RuntimeError())
print catches(Exception, KeyboardInterrupt())
print catches(None, Exception())
print catches(1, Exception())
_StopIteration = StopIteration
def f1():
import __builtin__
__builtin__.StopIteration = StopIteration = 0
# This should still work:
for i in xrange(5):
print i
__builtin__.StopIteration = _StopIteration
f1()
...@@ -81,6 +81,7 @@ def f2_2(): ...@@ -81,6 +81,7 @@ def f2_2():
print n, sys.exc_info()[0].__name__ print n, sys.exc_info()[0].__name__
print "done", n, sys.exc_info()[0].__name__ print "done", n, sys.exc_info()[0].__name__
print "after", n, sys.exc_info()[0].__name__
f2_2() f2_2()
def f3(): def f3():
...@@ -109,3 +110,51 @@ def f3(): ...@@ -109,3 +110,51 @@ def f3():
finally: finally:
print "outer finally" print "outer finally"
f3() f3()
def f4():
print
print "f4"
# This test answers the question of what the exact behavior of the "raise" (no argument) statement is,
# especially after a sub-exception has changed sys.exc_info
try:
try:
raise AttributeError()
except AttributeError:
try:
raise NotImplementedError()
except:
pass
# Even though lexically it looks like we're handling an AttributeError,
# at this point the "most recent exception" is a NotImplementedError,
# so when we "raise" we should throw that.
raise
except AttributeError:
print "caught attribute error (makes sense, but wrong)"
except NotImplementedError:
print "caught not implemented error (weird, but right)"
f4()
def f5():
print
print "f5"
# Based on what I learned from f4, I guess you can put a "raise" outside a try-catch block:
def inner():
try:
raise AttributeError()
except:
pass
print "reraising"
raise
try:
inner()
assert 0, "shouldn't get here"
except AttributeError:
print sys.exc_info()[0].__name__
f5()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment