0

Working with Python 3.7.3, still figuring out how exception handling works.

I'm writing an xmpp bot, using slixmpp. I'm trying to make it so that if it loses connection to the server, it will try to reconnect. There doesn't seem to be any way to do this built in to slixmpp, so I'm write something into my own code to do it.

I've imported slixmpp as xmpp, and using it's send_raw() method to test that we're still connected to the server.

 while True: time.sleep(5) # Send every 5 seconds just for testing purposes xmpp.send_raw('aroo?') 

When I sever the connection to the server, this is what it spits out:

 Traceback (most recent call last): File "C:\Program Files\Python37\lib\threading.py", line 917, in _bootstrap_inner self.run() File "testcom.py", line 19, in run eval(self.thing)() File "testcom.py", line 28, in check_conn xmpp.send_raw('aroo?') File "C:\Program Files\Python37\lib\site-packages\slixmpp\xmlstream\xmlstream.py", line 926, in send_raw raise NotConnectedError slixmpp.xmlstream.xmlstream.NotConnectedError 

I'm assuming that "NotConnectedError" is the exception that I need to catch, so I put the code inside a try block, like so:

 try: while True: time.sleep(5) # Send every 5 seconds just for testing purposes xmpp.send_raw('aroo?') except NotConnectedError: # Do a thing pass 

And this is what I get:

 Traceback (most recent call last): File "testcom.py", line 28, in check_conn xmpp.send_raw('aroo?') File "C:\Program Files\Python37\lib\site-packages\slixmpp\xmlstream\xmlstream.py", line 926, in send_raw raise NotConnectedError() slixmpp.xmlstream.xmlstream.NotConnectedError During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Program Files\Python37\lib\threading.py", line 917, in _bootstrap_inner self.run() "testcom.py", line 19, in run eval(self.thing)() File "testcom.py", line 29, in check_conn except NotConnectedError: NameError: name 'NotConnectedError' is not defined 

Can anyone tell me what I'm doing wrong here?

Thanks!

1 Answer 1

1

I can't see your imports but make sure you have from slixmpp.xmlstream.xmlstream import NotConnectedError otherwise it doesn't have a definition for NotConnectedError within the application. You could also change NotConnectedError to xmpp.xmlstream.xmlstream.NotConnectedError if you don't want to have it imported as well.

Sign up to request clarification or add additional context in comments.

1 Comment

That did it! I added the extra import and it's working perfectly. Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.