【问题标题】:Library File for exception handling in pythonpython中异常处理的库文件
【发布时间】:2012-10-31 07:25:57
【问题描述】:

我已经在 python 中编写了一个脚本,其中包含所有运行时异常的除处理(捕获块)。如果我将 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 块应该按照我的脚本而不是内置库执行..

【问题讨论】:

  • @shikhapanghal:编辑您的问题并将代码放在那里
  • 您的意思是您正在使用一些包含异常处理代码的库,但您想绕过该处理并在您的顶级异常处理程序中接收所有异常?

标签: python exception-handling


【解决方案1】:

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

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 并且两个文件都位于同一工作目录中)

try:
   import mymodule
   mymodule.main()
except SyntaxError as e:
   print("Exception found") # Optional: Add code to handle the exception

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

【讨论】:

  • 感谢您的回复..它非常详尽,但我不明白您提出异常的地方#### 提出在调用脚本中处理的异常####..dnt 得到这样做的目的/
  • 如果调用者有错误,你不能在被调用者中处理它们。异常会从被调用者传递给调用者,反之亦然,因此您的被调用者脚本不会知道发生了异常。
  • 感谢您的回复..它非常详尽,但我不明白您提出异常的地方#### 提出在调用脚本中处理的异常####..dnt 得到这样做的目的/
  • 该行采用SyntaxError 异常(您的except 语句的as e 部分将其别名为e。并再次引发它以调用代码以捕获。这允许您进行一些处理在发生异常的被调用代码中,还将异常处理责任委托给调用者。
猜你喜欢
  • 2014-08-05
  • 2021-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多