0

为什么名称解析错误没有被 EXCEPT 捕获?另外,我怎样才能获取错误文本并将其也扔到记录器设施中?谢谢你。

logging.basicConfig(filename = "{}.log".format(sys.argv[0]), level=logging.DEBUG)

def simpleLogger(something):
     try :
         something
         logging.info("Iteration fine")
     except :

         logging.warning("Something bad happened!")

 def doSometh():
     conn = psycopg2.connect(host = "1x18.249.21" , user = "tima",  dbname = "tima")
     curr = conn.cursor()
     sql = "select * from app_catalog;"

     curr.execute(sql)

     print curr.fetchall()

 def main():
    simpleLogger(doSometh())


 if __name__ == '__main__':
     sys.exit(main())

这是产生的错误:

conn = psycopg2.connect(host = "172.x18.249.21" , user = "tima",  dbname = "tima")
File "/apps/appeng/python-2.7.3/lib/python2.7/site-packages/psycopg2/__init__.py", line   179, in connect   connection_factory=connection_factory, async=async)
psycopg2.OperationalError: could not translate host name "172.x18.249.21" to address: Name or service not known
4

1 回答 1

1

如果这代表您的实际代码结构,那么您实际上是在将 doSomething() 函数传递给 simpleLogger 时调用它,而不是传入可调用对象然后调用它;因此执行不会发生在 try/except 块中。

于 2012-12-03T19:08:36.227 回答