MWE:
Код: Выделить всё
try:
conn_obj = Connect()
except Exception as e: # there are some other exceptions I want to ignore
if 'magicword' not in e.message:
print 'fatal error: {}'.format(e.message)
Однако на компьютере коллеги ошибка не обрабатывается, а вместо этого происходит сбой и создается обратная трассировка. На моем компьютере точно такая же ошибка, только он не печатается и вылетает вместо того, чтобы быть обработанным. Мы оба используем одну и ту же ОС (Windows 7).
Очевидно, что отказ от обработки конкретного исключения не идеален, поэтому я попробовал этот путь:
Код: Выделить всё
from urllib3.exceptions import NewConnectionError
try:
conn_obj = Connect()
except NewConnectionError as nce:
print 'fatal error: {}'.format(e.message)
except Exception as e: # there are some other exceptions I want to ignore
if 'magicword' not in e.message:
print 'fatal error: {}'.format(e.message)
ОБНОВЛЕНИЕ:
Объект подключения создается внутри сторонней библиотеки pyelasticsearch. Мне всегда удавалось его отловить, но на чужих машинах он не перехватывался с использованием того же кода. Вот файл, который я написал, чтобы проверить, была ли обнаружена ошибка при явном возникновении:
Код: Выделить всё
from urllib3.exceptions import NewConnectionError
def error_test(test_num):
print '\n\n'
try:
if test_num == 1:
print 'TEST 1: See if NewConnectionError is caught specifically'
raise NewConnectionError('no pool', 'test one')
elif test_num == 2:
print 'TEST 2: See if RuntimeError is caught related to magicword'
raise RuntimeError('test two magicword catching test')
elif test_num == 3:
print 'TEST 3: See if RuntimeError is caught NOT related to magicword'
raise RuntimeError('test three')
except NewConnectionError as nce:
print 'Test 1 passed successfully.\n\n{}'.format(nce.message)
except Exception as e:
if 'magicword' not in e.message:
print 'Test 3 passed successfully.\n\n{}'.format(e.message)
else:
print 'Test 2 passed successfully.\n\n{}'.format(e.message)
error_test(1)
error_test(2)
error_test(3)
Подробнее здесь: https://stackoverflow.com/questions/337 ... -not-on-an