Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python urllib3.exceptions.NewConnectionError错误在一台计算机上处理,但在另一台计算机上未处理_Python_Python 2.7_Exception Handling - Fatal编程技术网

Python urllib3.exceptions.NewConnectionError错误在一台计算机上处理,但在另一台计算机上未处理

Python urllib3.exceptions.NewConnectionError错误在一台计算机上处理,但在另一台计算机上未处理,python,python-2.7,exception-handling,Python,Python 2.7,Exception Handling,我有个奇怪的问题。为了有一个简短的最小工作示例(MWE),让我们假设Connect()返回一个urlib3.connection.HTTPConnection对象。我们还假设,如果在错误消息中发现'magicword'(不是实际的单词,但嘿,这是一个MWE),我还将忽略一些其他异常 MWE: try: conn_obj = Connect() except Exception as e: # there are some other exceptions I want to ignor

我有个奇怪的问题。为了有一个简短的最小工作示例(MWE),让我们假设
Connect()
返回一个
urlib3.connection.HTTPConnection
对象。我们还假设,如果在错误消息中发现
'magicword'
(不是实际的单词,但嘿,这是一个MWE),我还将忽略一些其他异常

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)
这在我的机器上运行良好,遇到“致命错误”时会打印出来,并忽略其他异常(在本例中应该如此)

但是,在同事的机器上,错误不会被处理,而是崩溃并创建回溯。这与我的机器上的错误完全相同,只是他的机器无法打印并崩溃,而没有得到处理。我们都使用完全相同的操作系统(Windows7)

显然,不处理特定异常并不理想,因此我尝试了该方法:

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)

这项测试在我们两台机器上都非常有效。因此,通过让第三方库参与进来,我们的机器之间存在一些不一致的地方(这实际上是在pyinstaller编译的二进制文件中,所以库的差异不应该起作用)。elasticsearch的文档

您能否提供您处理过的exc和同事的exc的回溯?你有没有试着打印出e.message,catch BaseException…?@kAlmAcetA当我调试它时,我们两人的回溯是相同的,只有我能捕获并处理异常,但他永远不会被捕获。我们都在运行相同的代码。要消除特定于lib的代码,请运行explicit
try:raisenewconnectioneror('no pool','my magicword');例外情况除外…
。或者更简单的带有消息的
异常
。它能用吗?@kAlmAcetA很抱歉耽搁了,我不得不和同事合作,写了一个简短的脚本,看看它是否捕捉到了这些错误。是的。当我显式地提出错误并进行处理时,这两台机器都能很好地处理它们。这个错误是通过pyelasticsearch库引发的,但是冒泡的异常是在我的机器上处理的,而不是在他的机器上。“看来我还有很多挖掘工作要做。”我添加了测试代码。