python-3.xexceptionspeech-recognitionexcept

How to fix "BaseException is not allowed"


I am trying to make this script get what im saying and print it out on terminal but im getting this error

TypeError: catching classes that do not inherit from BaseException is not allowed

for this I followed a tutorial and his worked just fine im running version 3.9.5 I have tried to look this up but nothing I found was helpful if you know please let me know

 import speech_recognition
    import pyttsx3
    
    recognizer = speech_recognition.Recognizer()
    
    while True:
        try:
            with speech_recognition.Microphone() as mic:
                recognizer.adjust_for_ambient_noise(mic, duration=0.2)
                audio = recognizer.listen(mic)
    
                text = recognizer.recognize_google(audio)
                text = text.lower()
    
                print(f"Recognized {text}")
    
        except speech_recognition.UnknownValueError():
            recognizer = speech_recognition.Recognizer()
            continue

Solution

  • Your

    except speech_recognition.UnknownValueError():
    

    should be

    except speech_recognition.UnknownValueError:
    

    i.e. it should name the type, not call it and use the return value.