I use python 3.10. HTTPError is no good. Use from urllib.error import URLError
from urllib.error import URLError
from urllib.error import URLError try: urlopen('http://url404.com') except URLError as e: print(e.code)