页: 1 我的文字中的条款。 能否从<条码>中获取准确的错误信息?
我能否从晚上最后的班子那里获得例外吗?
原标题:Can I get the exception from the finally block in python?
最佳回答
No, at finally
time sys.exc_info
is all-None, whether there has been an exception
or not. Use:
try:
whatever
except:
here sys.exc_info is valid
to re-raise the exception, use a bare `raise`
else:
here you know there was no exception
finally:
and here you can do exception-independent finalization
问题回答
“finally
栏目将予执行,不论是否提出了例外,例如,见习员指出,你很可能不想在那里处理。
如果你真的需要提出的例外的价值,那么你就应当把例外情况排在<条码>之外,或者适当处理或重新处理,然后在最后部分使用这一价值——如果在执行期间没有出现任何例外,则可能永远不会确定。
import sys
exception_name = exception_value = None
try:
# do stuff
except Exception as e:
exception_name, exception_value, _ = sys.exc_info()
raise # or don t -- it s up to you
finally:
# do something with exception_name and exception_value
# but remember that they might still be none
实际上,其他答复含糊不清。 因此,让我澄清这一点。 你们总是能够从最后的脚石中援引ys。 然而,其产出将因是否实际提出了例外而有所不同。
import sys
def f(i):
try:
if i == 1:
raise Exception
except Exception as e:
print "except -> " + str(sys.exc_info())
finally:
print "finally -> " + str(sys.exc_info())
f(0)
f(1)
>>>
finally -> (None, None, None)
except -> (<type exceptions.Exception >, Exception(), <traceback object at 0x029438F0>)
finally -> (<type exceptions.Exception >, Exception(), <traceback object at 0x029438F0>)
因此,如果是发挥第一级职能,你总是能够最终知道是否提出了例外。 但是,如以下例子所示,当电话停留时间超过1时,就会有不同的表现。 详情请上。 How sys.exc_info() work?
import sys
def f(i):
try:
if i == 1:
raise Exception
except Exception as e:
print "except -> " + str(sys.exc_info())
finally:
print "finally -> " + str(sys.exc_info())
def f1(i):
if i == 0:
try:
raise Exception( abc )
except Exception as e:
pass
f(i)
f1(0)
f1(1)
>>>
finally -> (<type exceptions.Exception >, Exception( abc ,), <traceback object at 0x02A33940>)
except -> (<type exceptions.Exception >, Exception(), <traceback object at 0x02A33990>)
finally -> (<type exceptions.Exception >, Exception(), <traceback object at 0x02A33990>)
我希望,它使事情更加清楚。
编号:
finally
timesys.exc_info
是全程的,无论有无例外。 使用[相反]:......
其他回答者是正确的,因为你should正在,但
条款内处理。
然而,对于职位/记录,本文是对下述原始问题的答复:
import sys
try:
int("not an integer LOL")
except:
e = sys.exc_info()[1]
# isinstance(e, ValueError) == True
raise # this line is optional; I have it commented for the example output
else:
e = None # you should do this to avoid a NameError
finally:
print("I really wanted to access %s inside of a finally clause. And I m doing so now."
% repr(e))
这将印刷如下内容:
我真的想在最后条款内查阅<代码>Value Error(“无效字面”),其基数为10:不为LOL“。 现在我这样做了。
除条款外,你希望这样做,而不是最后。
参考:。 http://www.doughellmann.com/articles/Python-Exception-Handling/
相关问题
热门标签
- winforms
- combobox
- fogbugz
- java
- date
- internationalization
- asp.net
- iis
- url-rewriting
- urlrewriter
- c#
- enums
- ocaml
- haxe
- algorithm
- string
- viewstate
- .net
- c++
- c
- symbol-table
- mysql
- database
- postgresql
- licensing
- migration
- vb.net
- vb6
- declaration
- vb6-migration
- python
- psycopg2
- backup
- vmware
- virtualization
- gnu-screen
- authentication
- desktop
- excel
- xll
- cultureinfo
- regioninfo
- oracle
- client
- session
- download
- html
- virtual
- constructor
- scenarios
- perl
- full-text-search
- javascript
- ajax
- testing
- oop
- inheritance
- vim
- encapsulation
- information-hiding