我有来自Python 2.4的代码,并希望在Python 2.6上编译此代码,但我看到了这个错误:
>>> py_compile.compile("Model.py")
SyntaxError: ('invalid syntax', ('pcbModel.py', 73, 19, ' msg = "%s: %s. The error occurred generating \'%s\'." % (sys.exc_type, sys.exc_value, bName)\n'))
,代码是:
try:
pcb.Create(self.skipMeshing, analysisType = self.analysisType)
msg = "%s: %s. The error occurred generating '%s'." % (sys.exc_type, sys.exc_value, bName)
raise Exception, msg
continue
self.deactivate(bName)
如何解决?
答案 0 :(得分:2)
看起来你有一个try
子句,没有相应的except
。另外,raise type, args
表单已弃用,请使用raise type(args)
。另外,sys.exc_type
和朋友不是线程安全的。语法正确的版本是:
# DON'T DO THIS, SEE BELOW
try:
pcb.Create(self.skipMeshing, analysisType = self.analysisType)
except Exception as e:
msg = "%s: %s. The error occurred generating '%s'." % (type(e), e, bName)
raise Exception(msg)
然而,看起来好像你正试图捕捉"异常,计算某种错误消息并引发异常。例外已经这样做了。实际上,上面的idomatic版本,
pcb.Create(self.skipMeshing, analysisType = self.analysisType)
没有try
,没有except
,没有raise
。如果pcb.Create()
引发异常,则已经提出异常,您不需要再提出异常。