The title is pretty self explanatory -- what's the difference between:
raise Exception, "foo"
and
raise Exception("foo")
Does it do exactly the same thing, just different syntax?
I'm using Python 2.x, but I'd like to hear of any differences in Python 3.x
Raise Exception is when you find an error and want to raise it. Exception handler is to catch exceptions raised by you or the system and handle them gracefully.
You use try to delineate the block in which you expect an exception may occur. You use catch to specify what to do when an exception is raised. And you use throw to raise an exception yourself.
You can't handle a exception which wasn't raised. except is how you handle an exception that some other code signalled. raise is how you signal an exception yourself. It's like asking what the difference is between making a phone call and answering the phone.
Let's consider a situation where we want to raise an exception in response to catching a different exception but want to include information about both exceptions in the traceback. To chain exceptions, use the raise from statement instead of a simple raise statement. This will give you information about both errors.
both amount to the same thing in Python2. in Python3, the raise Exception, "foo"
syntax is no longer supported.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With