S
SM
try:
x = 1/0
except ZeroDivisionError:
print "caught ZeroDivisionError"
try:
name_error
except NameError:
print "caught NameError"
raise
According to the Python Language Reference
http://docs.python.org/ref/raise.html
'raise' re-raises the last expression that was active in the current
scope. Here apparently "scope" means some sort of lexical scope,
because this program terminates with a NameError exception and not a
ZeroDivisionError.
It seems useful to have a way to raise the exception that is currently
being handled (ZeroDivisionError in this case) and not any exception
that happened to be caught earlier in the current exception handler.
For example,
try:
network.command()
except:
try:
network.close()
except:
pass
raise
I want to make a network call, and if it raises an exception, I want
to close the network connection before allowing the exception to
propagate up the stack. However, trying to close the network
connection might itself raise an exception, and I don't care about
that exception; I want to send the original exception. Apparently the
way to do this is:
try:
network.command()
except Exception, bob:
try:
network.close()
except:
pass
raise bob
x = 1/0
except ZeroDivisionError:
print "caught ZeroDivisionError"
try:
name_error
except NameError:
print "caught NameError"
raise
According to the Python Language Reference
http://docs.python.org/ref/raise.html
'raise' re-raises the last expression that was active in the current
scope. Here apparently "scope" means some sort of lexical scope,
because this program terminates with a NameError exception and not a
ZeroDivisionError.
It seems useful to have a way to raise the exception that is currently
being handled (ZeroDivisionError in this case) and not any exception
that happened to be caught earlier in the current exception handler.
For example,
try:
network.command()
except:
try:
network.close()
except:
pass
raise
I want to make a network call, and if it raises an exception, I want
to close the network connection before allowing the exception to
propagate up the stack. However, trying to close the network
connection might itself raise an exception, and I don't care about
that exception; I want to send the original exception. Apparently the
way to do this is:
try:
network.command()
except Exception, bob:
try:
network.close()
except:
pass
raise bob