try: except: not working

妖精的绣舞 提交于 2019-12-05 17:13:36

问题


So I'm running into a problem where the try: except: mechanism doesn't seem to be working correctly in python.

Here are the contents of my two files.

pytest1.py

import pytest2

class MyError( Exception ):
    def __init__( self, value ):
        self.value = value

    def __str__( self ):
        return repr( self.value )

def func1():
    raise MyError( 'This is an error' )

def func3():
    pytest2.func2()

if __name__ == '__main__':
    try:
        func3()
    except MyError, e:
        print 'I should catch here.'
    except:
        print 'Why caught here?'

pytest2.py

from pytest1 import func1

def func2():
    func1()

Executing the first file yields the following output:

$ python pytest1.py
Why caught here?

Basically, the exception isn't being caught. If I print out the exception type, it prints as <pytest1.MyError> instead of just <MyError>. I imagine that this is some weird cyclical reference thing, but it still seems like it should work.


回答1:


The main python program is always imported as the module __main__.

When you import pytest2, it doesn't reuse the existing module because the originally imported module has the name __main__ not pytest2. The result is that pytest1 is run multiple times generating multiple exception classes. __main__.MyError and pytest1.MyError You end up throwing one and trying to catch the other.

So, don't try to import your main module from other modules.




回答2:


This problem is caused by importing the script you are running as a module. This produces two separate copies of the module!

Another example:

module.py

import module

class Foo: pass

def test():
    print Foo
    print module.Foo
    print Foo is module.Foo

if __name__ == '__main__': test()

main_script.py

import module
if __name__ == '__main__': module.test()

Result

>python main_script.py
module.Foo
module.Foo
True

>python module.py
__main__.Foo
module.Foo
False

Running python somefile.py creates a module called __main__, not somefile, and runs the code in somefile.py in that module. This is why if __name__ == '__main__': is used to check if this file is being run as a script or imported from some other file.




回答3:


... at a guess, you have a namespace problem which is producing a different exception.

Try replacing

except:
    print 'Why caught here?'

with

except Exception, e:
    print e

This may tell you more about what went wrong.



来源:https://stackoverflow.com/questions/4952134/try-except-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!