python中异常处理的库文件

python中异常处理的库文件,python,exception-handling,Python,Exception Handling,我用python编写了一个脚本,它对所有运行时异常都有except处理(catch block)。如果我将try块放在与脚本相同的文件中,那么它将打印异常,但我需要的是,如果try块位于不同的文件中,那么它将使用脚本中编写的catch块的过程是什么 import traceback import sys import linecache try: # execfile(rahul2.py) def first(): second() def sec

我用python编写了一个脚本,它对所有运行时异常都有except处理(catch block)。如果我将try块放在与脚本相同的文件中,那么它将打印异常,但我需要的是,如果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块都应该按照我的脚本执行,而不是按照内置库执行。

如果要在调用此异常的脚本中处理此异常,则需要引发异常。例如:

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 , "'")
       #### Raise up the exception for handling in a calling script ####
       raise e
然后在调用脚本中,您只需放置另一个try-except块(假设您编写的“库文件”名为mymodule.py,并且两个文件都位于同一工作目录中),如下所示


请记住,如果无法处理此重新引发的异常,将导致脚本失败并退出(打印异常消息和堆栈跟踪)。这是一件好事。如果恢复方法未知或无法以编程方式处理,则遇到致命错误的脚本应以引起用户注意的方式失败。

@shikhapanghal:编辑您的问题并将代码放在那里您的意思是您正在使用一些包含异常处理代码的库吗,但是你想绕过这个处理,在你的顶级异常处理程序中接收所有异常吗?谢谢你的回复。这很详细,但我不明白你在调用脚本中处理异常的目的是什么/如果调用方有错误,你无法处理他们在被叫人的房间里。异常是从被调用方传递到调用方的,而不是从被调用方传递到调用方的,因此您的被调用方脚本将不知道发生了异常。谢谢您的回复..非常详细,但我不明白您提出异常的目的是什么###########################
SyntaxError
异常(except语句的
as e
部分将其别名为
e
。并再次引发该异常以调用代码捕获。这允许您在发生异常的被调用代码中执行某些处理,但也将异常处理责任委托给调用方。
try:
   import mymodule
   mymodule.main()
except SyntaxError as e:
   print("Exception found") # Optional: Add code to handle the exception