cv2.VideoCapture(rtsp://username:password@IP Address:554/stream1) freezes

The following Python code works fine when capturing image frames an IP camera:

cv2.VideoCapture(rtsp://username:password@IP_Address:554/stream1)

But the app crashes/freezes when using a wrong IP address or a wrong port number (instead of 554). Interestingly, the app does not crash when using a wrong username or a wrong password, simply failing to open the camera (i.e., cap.isOpened() is False).

Is there a way to avoid the app from crashing (e.g., aborting the above code) even when using a wrong IP address or a wrong port number?

Here is my PC environment:
OS: Windows 10
Python version: 3.6
OpenCV version: 4.5.0

Thanks