简体   繁体   English

如何在python中的try-except循环之前记录错误?

[英]How to log errors before the try- except loop in python?

I am trying to implement Python's logging library for log. 我正在尝试实现Python的日志记录库。

I understand that if something is wrapped in try-catch loop, the exception is caught. 我了解,如果将某些内容包装在try-catch循环中,则会捕获异常。

But in one of my example, the import is failing which is the start of the script. 但是在我的一个示例中,导入失败,这是脚本的开始。 This is not getting logged. 这没有得到记录。 ( It is only printing this in Jupyter's logs) (仅在Jupyter的日志中打印此内容)

How to log these exceptions? 如何记录这些异常? Looking for not wrapping the entire script in try - except loop. 寻找不将整个脚本包装在try -except循环中。

Any error on Jupyter or any IDE is printed in the console, why not with logger? 控制台上会打印Jupyter上的任何错误或任何IDE,为什么不使用logger? isnt there any such implementation 没有任何这样的实现

When using logger, you have to implement the logging of your errors. 使用记录器时,必须实现错误记录。
In Python, errors are being logged only to the console by default. 在Python中,默认情况下,错误仅记录到控制台。
So, if you want to use logger, you have to add your logic to catch and log your errors. 因此,如果要使用记录器,则必须添加逻辑以捕获和记录错误。

The try except block is a common way to handle import on Python. try except块是在Python上处理导入的一种常用方法。

Quoting Dive into Python : 引用Dive进入Python

There are a lot of other uses for exceptions besides handling actual error conditions. 除了处理实际的错误情况外,异常还有很多其他用途。 A common use in the standard Python library is to try to import a module, and then check whether it worked . 标准Python库中的常见用法是尝试导入模块,然后检查其是否正常工作 Importing a module that does not exist will raise an ImportError exception. 导入不存在的模块将引发ImportError异常。 You can use this to define multiple levels of functionality based on which modules are available at run-time, or to support multiple platforms (where platform-specific code is separated into different modules). 您可以使用它基于运行时可用的模块定义多个功能级别,或支持多个平台(特定于平台的代码分为不同的模块)。

The next example demonstrates how to use an exception to support platform-specific functionality. 下一个示例演示如何使用异常来支持特定于平台的功能。

try:
    import termios, TERMIOS                     
except ImportError:
    try:
        import msvcrt                           
    except ImportError:
        try:
            from EasyDialogs import AskPassword 
        except ImportError:
            getpass = default_getpass           
        else:                                   
            getpass = AskPassword
    else:
        getpass = win_getpass
else:
    getpass = unix_getpass

The difference with IDE's logs or even if you run a python file from console or terminal is that as soon as the exception is caught the script is interrupted immediately. 与IDE日志的区别甚至是从控制台或终端运行python文件的区别在于,一旦捕获到异常,脚本将立即中断。

If you want to get the exception and do something after it happens, to log it for instance, then you need to use the "try except" block. 如果要获取异常并在异常发生后执行某些操作,例如要记录该异常,则需要使用“ tryexcept”块。

I don't know why you are trying to avoid using the "try except" block as it is a basic feature of the language as any other decision-making blocks ("if", "while", "for", etc.). 我不知道您为什么要避免使用“ tryexcept”块,因为它与其他任何决策块(“ if”,“ while”,“ for”等)一样是语言的基本功能。 。

Try to see it as a common "if" statement: 尝试将其视为常见的“ if”语句:

if trying this:
    works, great!
else:
    do something with this exception

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM