Search code examples
python-3.xraspbian

Getting Exception in tread Thread-1 where a try should catch the error


I am using Wifi module and using this helper file I want to connect to wifi with the function Connect() it does work but when there is a problem the function should return False but for some reason, I am getting this error wifi.exceptions.ConnectionError.... as I can tell the helper file should catch that error and then return False to my main program. As stated in line 57 and 69 except wifi.exceptions.ConnectionError: instead it is just crashing my program.

 import WifiHandler

 isConnected = WifiHandler.Connect("wifiNetwork","Password")

 if isConnect == True:
      do Somthing 

and this is the error

TraceBack (most recent call last):
 File "test.py". line 11, in <module>
  isConnected = WifiHandler.Connect(ssid,password)
 File "/home/pi/WifiHandler.py", line 43, in Connect
    savedcell.activate()
 File "/usr/local/lib/python3.4/dist-packages/wifi/scheme.py", line 176. in activate
     return self.parse_ifup_output(ifup_output)
 File "/usr/local/lib/python3.4/dist-packages/wifi/scheme.py" line 183, in parse_ifuo_output
     raise ConnectionError("failed to connect to %r" % self)
     wifi.exceptions.ConnectionError: Failed to connect to Scheme(interface='wlan0',name ='SSIDName', options={'wpa-psk':'32nnj3323jej222n4n2n421353535353535353535353535353', 'wpa-ssid': 'SSIDName' , 'wireless-channel':'auto'})

Solution

  • Hey guys thank to Morb. I fix the problem.

    In the helper file it was not catching the error for savedcells

          if savedcell:
                savedcell.activate()
                return cell
    

    it should be

     if savedcell:
            try:
               savedcell.activate()
            except wifi.exceptions.ConnectionError:
    
                return False
    
            return cell