Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between raise Exception, "foo" and raise Exception("foo")?

Tags:

python

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

like image 586
bradley.ayers Avatar asked Jun 23 '11 00:06

bradley.ayers


People also ask

What is the difference between raising an exception and handling an exception?

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.

What is the difference between throw catch and raise rescue?

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.

What is the difference between raising an exception and handling an exception Python?

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.

How do you raise an exception from another exception?

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.


1 Answers

both amount to the same thing in Python2. in Python3, the raise Exception, "foo" syntax is no longer supported.

like image 70
jcomeau_ictx Avatar answered Sep 24 '22 11:09

jcomeau_ictx