python异常处理
在下面的python中,消息RSU在单节点机器上不受支持**未被打印。有人能帮忙吗python异常处理,python,Python,在下面的python中,消息RSU在单节点机器上不受支持**未被打印。有人能帮忙吗 #! /usr/bin/env python import sys class SWMException(Exception): def __init__(self, arg): print "inside exception" Exception.__init__(self, arg) class RSUNotSupported(SWMException):
#! /usr/bin/env python
import sys
class SWMException(Exception):
def __init__(self, arg):
print "inside exception"
Exception.__init__(self, arg)
class RSUNotSupported(SWMException):
def __init__(self):
SWMException.__init__(self, "**RSU is not supported on single node machine**")
def isPrepActionNeeded():
if 1==1:
raise RSUNotSupported()
try:
isPrepActionNeeded()
except:
sys.exit(1)
因为您使用try/except子句处理异常。它没有打印,因为您甚至没有尝试打印它:)这里:
将最后两行更改为:
except Exception as e:
print e
sys.exit(1)
我在这里使用just
Exception
来保持它与裸Exception:
的等效性。你真的应该使用RSUNotSupported
,这样你就不会隐藏其他类型的错误。异常。\uuuu init\uuuuuuu(self,arg)这里传递的arg。那是干什么的?嗯,你必须把“**RSU…”的信息放在某个地方,对吗?您的异常继承自exception类,因此为exception.\uuuu init\uuuu(self,message)初始化超类并将参数(消息)传递给它。现在,您的异常包含该消息,并且调用RSUNotSupported类实例(或该实例上的str())的_ustr _;()将返回该消息。异常。_init __;(self,arg)此init调用中的arg参数用于什么??它不会打印消息,而是将消息作为异常的一部分,因此,如果需要,您可以稍后自己打印消息。
except Exception as e:
print e
sys.exit(1)