Do I need a try/finally clause when calling cv2.VideoCapture.release()?
07:25 01 Nov 2026

I'm learning how to capture video from camera using opencv for Python. The official tutorial provides the following example (modified):

import cv2 as cv

cap = cv.VideoCapture(0)
if not cap.isOpened():
    print("Cannot open camera")
    exit()

while True:
    # Capture frame-by-frame
    ret, frame = cap.read()

    # if frame is read correctly ret is True
    if not ret:
        print("Can't receive frame (stream end?). Exiting ...")
        break

    # Display the resulting frame
    cv.imshow('frame', gray)
    if cv.waitKey(1) == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv.destroyAllWindows()

According to this question, the final cap.release() call is important because it releases the hardware and software resource. In that case, why is it not wrapped in a try/finally clause?

This answer, which provides a context manager for reading VideoCapture, does use try/finally.

Considering how many exceptions I get while playing around with opencv, I've missed the final cap.release() call dozens of times already.

python opencv