我用python编写了一个脚本,它在整个运行时都有except handling(catch block)例外情况。如果我把try块放在和script相同的文件中,然后它会打印例外,但是我需要的是如果try块在不同的文件中,那么它将使用脚本中写入的catch块的过程是什么。在import traceback
import sys
import linecache
try:
# execfile(rahul2.py)
def first():
second()
def second():
i=1/0;
def main():
first()
if __name__ == "__main__":
main()
except SyntaxError as e:
exc_type, exc_value, exc_traceback = sys.exc_info()
filename = exc_traceback.tb_frame.f_code.co_filename
lineno = exc_traceback.tb_lineno
line = linecache.getline(filename, lineno)
print("exception occurred at %s:%d: %s" % (filename, lineno, line))
print("**************************************************** ERROR ************************************************************************")
print("You have encountered an error !! no worries ,lets try figuring it out together")
print(" It looks like there is a syntax error in the statement:" , formatted_lines[2], " at line number " , exc_traceback.tb_lineno)
print("Make sure you look up the syntax , this may happen because ")
print(" Remember this is the error message always thrown " "'" ,e , "'")
同样的,我也为其他例外写过。。。在
现在我的问题是,假设我想对所有程序使用这个脚本,或者假设try块在一个不同的文件中…那么我如何将我的脚本和有try块的程序链接起来。。在
或者如果我用不同的词来表达,那么我想要的是每当有try-catch块时,catch块应该按照我的脚本而不是内置的库来执行。。在