Solution for How to catch error from bluetooth socket and print custom message?
is Given Below:
I have a question, I’m a beginner in python, and I have a bluetooth connection script which I would like to amend for catching the error (if an error occurs).
The script (it works):
sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM ) sock.connect((bd_addr, port)) print('Succeed!') sock.close()
How could I catch it? I get this if host is down:
bluetooth.btcommon.BluetoothError: [Errno 112] Host is down
and I would like to print only my own error msg.
The usual way in Python would be to use
except something like this:
sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM) try: sock.connect((bd_addr, port)) except bluetooth.btcommon.BluetoothError: print('Host is down') else: print('Succeed!') sock.close()