Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

raise ConnectionError in python2.7

i want to raise ConnectionError (a python 3 subclass) when my program fails to connect to a local service. i'm using python2.7. testing this out though, it looks like i have to import a module to get this working:

>>> raise ConnectionError("test")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'ConnectionError' is not defined

how can i raise this type of exception?

like image 361
mulllhausen Avatar asked Sep 12 '26 02:09

mulllhausen


1 Answers

ConnectionError does not exist in Python 2, as you can see from the Python 2 documentation. You can of course create your own subclass of OSError (and even call it ConnectionError if you want), but no such thing is built in.

like image 81
BrenBarn Avatar answered Sep 13 '26 14:09

BrenBarn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!