Python Forum

Full Version: PiCamera - print exceptions?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello ,
I'm trying to handle exception but it said
NameError: name 'Picamera' is not defined
what did I miss?

from picamera import PiCamera

try:
        camera = PiCamera()
except picamera.PiCameraError:
        print('Camera problem')
        return ('Camera problem')
except picamera.PiCameraMMALError:
        print('error1')
        return ('ERRRORR')
except Exception as e:
        print(e)
        return(e)
Thanks ,
You imported PiCamera. Nowhere do you import picamera.
what DeanStad is telling you is that lines 5 and 8 are referencing the Package name and not the instance.
for example, line 5 should read except PiCamera.PiCameraError:
same for line 8

A better way, and the one recommended by the author would be:
import picamera


with picamera.PiCamera() as camera:
    try:
        camera.brightness = int(some_user_value)
    except PiCameraError:
        print('Something went wrong with the camera')
    # etc.
assign exceptions to the individual actions, don't try to capture all in one place.
see: https://picamera.readthedocs.io/en/relea...i_exc.html