提问人:Jack McGowan 提问时间:6/29/2023 最后编辑:Jack McGowan 更新时间:6/29/2023 访问量:67
即使使用 try/except 语句捕获 Python 中的异常,程序也有可能崩溃吗?
Is it possible for program to crash even with try/except statement that captures Exception in Python?
问:
我正在创建一些自动化,可以在某个时间点重新启动设备。为了在重新启动后重新连接,我想在等待一段时间后继续尝试连接,直到它最终重新连接。由于重新连接失败时会出现错误,因此我认为我可以捕获所有异常,然后只是休眠一段时间,然后再次运行连接代码并继续此循环。但是,我的程序在到达连接错误后仍然崩溃。是否只是有一些例外情况无法捕捉到?
这是我为参考而创建的函数,但我仍然只是收到一堆不同的错误,例如 、 、 和requests.exceptions.ConnectionError
urllib3.exceptions.MaxRetryError
urllib3.exceptions.NewConnectionError
ConnectionRefusedError
def establish_session(device):
try:
sess = IdentityServicesEngineAPI(username=config.username,
password=config.password,
base_url='https://' + device,
verify=False)
except Exception as e:
print("connection refused. node rebooting. Trying again in 60 seconds")
time.sleep(60)
return establish_session(device) # Nested here...
return sess
任何反馈将不胜感激
更新:添加完整回溯。
Traceback (most recent call last):
File "/python3.10/site-packages/urllib3/connection.py", line 174, in _new_conn
conn = connection.create_connection(
File "/python3.10/site-packages/urllib3/util/connection.py", line 95, in create_connection
raise err
File "/python3.10/site-packages/urllib3/util/connection.py", line 85, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 61] Connection refused
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/python3.10/site-packages/urllib3/connectionpool.py", line 714, in urlopen
httplib_response = self._make_request(
File "/python3.10/site-packages/urllib3/connectionpool.py", line 403, in _make_request
self._validate_conn(conn)
File "/python3.10/site-packages/urllib3/connectionpool.py", line 1053, in _validate_conn
conn.connect()
File "/python3.10/site-packages/urllib3/connection.py", line 363, in connect
self.sock = conn = self._new_conn()
File "/python3.10/site-packages/urllib3/connection.py", line 186, in _new_conn
raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPSConnection object at 0x104e2b670>: Failed to establish a new connection: [Errno 61] Connection refused
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/python3.10/site-packages/requests/adapters.py", line 486, in send
resp = conn.urlopen(
File "/python3.10/site-packages/urllib3/connectionpool.py", line 798, in urlopen
retries = retries.increment(
File "/python3.10/site-packages/urllib3/util/retry.py", line 592, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='device1', port=443): Max retries exceeded with url: /api/v1/task (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x104e2b670>: Failed to establish a new connection: [Errno 61] Connection refused'))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/python3.10/site-packages/ciscoisesdk/restsession.py", line 497, in request
response = self._req_session.request(method, abs_url, **kwargs)
File "/python3.10/site-packages/requests/sessions.py", line 589, in request
resp = self.send(prep, **send_kwargs)
File "/python3.10/site-packages/requests/sessions.py", line 703, in send
r = adapter.send(request, **kwargs)
File "/python3.10/site-packages/requests/adapters.py", line 519, in send
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='device1', port=443): Max retries exceeded with url: /api/v1/task (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x104e2b670>: Failed to establish a new connection: [Errno 61] Connection refused'))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/python3.10/site-packages/urllib3/connection.py", line 174, in _new_conn
conn = connection.create_connection(
File "/python3.10/site-packages/urllib3/util/connection.py", line 95, in create_connection
raise err
File "/python3.10/site-packages/urllib3/util/connection.py", line 85, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 61] Connection refused
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/python3.10/site-packages/urllib3/connectionpool.py", line 714, in urlopen
httplib_response = self._make_request(
File "/python3.10/site-packages/urllib3/connectionpool.py", line 403, in _make_request
self._validate_conn(conn)
File "/python3.10/site-packages/urllib3/connectionpool.py", line 1053, in _validate_conn
conn.connect()
File "/python3.10/site-packages/urllib3/connection.py", line 363, in connect
self.sock = conn = self._new_conn()
File "/python3.10/site-packages/urllib3/connection.py", line 186, in _new_conn
raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPSConnection object at 0x104e29930>: Failed to establish a new connection: [Errno 61] Connection refused
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/lib/python3.10/site-packages/requests/adapters.py", line 486, in send
resp = conn.urlopen(
File "/python3.10/site-packages/urllib3/connectionpool.py", line 798, in urlopen
retries = retries.increment(
File "/python3.10/site-packages/urllib3/util/retry.py", line 592, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='device1', port=443): Max retries exceeded with url: /api/v1/task (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x104e29930>: Failed to establish a new connection: [Errno 61] Connection refused'))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/lib/python3.10/site-packages/ciscoisesdk/restsession.py", line 504, in request
response = self._req_session.request(method, abs_url,
File "/python3.10/site-packages/requests/sessions.py", line 589, in request
resp = self.send(prep, **send_kwargs)
File "/python3.10/site-packages/requests/sessions.py", line 703, in send
r = adapter.send(request, **kwargs)
File "/python3.10/site-packages/requests/adapters.py", line 519, in send
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='device1', port=443): Max retries exceeded with url: /api/v1/task (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x104e29930>: Failed to establish a new connection: [Errno 61] Connection refused'))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Pre_Post_Checks_Automation/test_api_calls.py", line 5, in <module>
hold = api.tasks.get_task_status().response
File "python3.10/site-packages/ciscoisesdk/api/v3_1_patch_1/tasks.py", line 123, in get_task_status
_api_response = self._session.get(endpoint_full_url, params=_params)
File "/python3.10/site-packages/ciscoisesdk/restsession.py", line 609, in get
response = self.request('GET', url, erc, 0, params=params, **kwargs)
File "python3.10/site-packages/ciscoisesdk/restsession.py", line 507, in request
raise ciscoisesdkException('Socket error {}'.format(e))
ciscoisesdk.exceptions.ciscoisesdkException: Socket error HTTPSConnectionPool(host='device1', port=443): Max retries exceeded with url: /api/v1/task (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x104e29930>: Failed to establish a new connection: [Errno 61] Connection refused'))
答:
1赞
juanpethes
6/29/2023
#1
要回答标题中的问题,是的,有些例外不是.例如,和 不是 的子类,而是 的子类,这意味着它们不会被子句捕获。Exception
GeneratorExit
KeyboardInterrupt
SystemExit
Exception
BaseException
except Exception
有关内置异常的层次结构,请参阅此内容:
https://docs.python.org/3/library/exceptions.html#exception-hierarchy
以及来自 mCoding 的这个非常有用的视频:
评论
establish_session(device)
return sess
return establish_session(device) # Nested here...
Exception
GeneratorExit
KeyboardInterrupt
SystemExit
Exception
BaseException
except Exception