如何使python在错误后不关闭自我,python异常

发布于 2025-02-11 04:34:32 字数 164 浏览 1 评论 0原文

我已经在Python中制作了一个程序,它使用自动py-to-exe转换为.exe,并且我想知道如何停止。Exe在错误后停止自我关闭(python exceptions)。光线,您无法阅读错误。

有人知道如何使它不关闭自己吗?

如果例外发生在PIP库中,请使用输入

不起作用

I have made an program in python that i converted into .exe using auto-py-to-exe and im wondering how to stop .exe stop closing it self after an error (python exception) the .exe closes it self in the speed of light and you cant read the error.

Does someone knows how to make it not close it self?

using input doesnt work if the exception happens in a pip library

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无尽的现实 2025-02-18 04:34:32

您可以使用基于试验的系统(您必须适当地构建以捕获代码的每个例外),并且要打印异常,您可以使用模块追溯这样:

try:
    ## code with error ##
except Exception:
    print("Exception in user code:")
    print("-"*60)
    traceback.print_exc(file=sys.stdout)
    print("-"*60)

或者您可以使用更简单的表格,例如:

try:
    ## code with error ##
except Exception as e:
    print(e, file='crash log.txt')

只能打印出来错误类(例如找不到文件)。

我还应该指出,终于存在关键字,目的是执行代码是否出现,是否出现:

try:
    ## code with error ##
except:                                             #optional
    ## code to execute in case of exception ##
finally:
    ## code executed either way ##

您可以在此处执行的某些内容是记录程序使用

print的 所有内容(status_of_program,file = open('log.txt','a'))

这很有用,可以准确查看程序崩溃的时间,并且通常可以逐步查看程序中的程序。

但是,您应该做的一件事是在.py形式的情况下正确测试程序,如果起作用,您可能会假设错误来自实际的出口方法,并咨询文档或尝试导出更简单的程序以捕获差异(因此,错误)。

我建议咨询:
https://docs.python.org/3/library/library/lebibrary/exceptions.html
学习错误类型和Try-Excect构造。


如果输入()不起作用,请尝试使用sys.stdout()

You can use a try-except based system (that you have to build suitably to catch every exception of your code) and to print the exception you can use the module traceback like so:

try:
    ## code with error ##
except Exception:
    print("Exception in user code:")
    print("-"*60)
    traceback.print_exc(file=sys.stdout)
    print("-"*60)

or you could use a simpler form like:

try:
    ## code with error ##
except Exception as e:
    print(e, file='crash log.txt')

that only prints the error class (like file not found).

I should also point out that the finally keyword exists with the purpose of executing code either if an exception arose or not:

try:
    ## code with error ##
except:                                             #optional
    ## code to execute in case of exception ##
finally:
    ## code executed either way ##

Something you could do on top of that is logging everything your program does with

print(status_of_program, file=open('log.txt','a'))

this is useful to see exactly at what point the program has crashed and in general to see the program in action step by step.

But a thing you should do is properly test the program while in .py form and if it works you could possibly assume the error comes from the actual exportation method and consult the documentation or try exporting simpler programs to catch the difference (and so the error).

i'd suggest to consult:
https://docs.python.org/3/library/exceptions.html
to learn the error types and the try-except construct.


If input() doesn't work try using sys.stdout().

一枫情书 2025-02-18 04:34:32

我认为这是一个控制台应用。如果是GUI应用程序,则可以使用类似的方法,但是细节会有所不同。

我使用的方法是将sys.excepthook设置为打印异常的您自己的功能,然后等待输入。您可以调用现有的异常挂钩实际进行打印。

import sys, traceback as tb

oldhook = sys.excepthook

def waitexcepthook(type, exception, traceback):
    oldhook(type, exception, traceback)
    input()

sys.excepthook = waitexcepthook

I gather that this is a console app. If it's a GUI app, you can use a similar approach but the details will be different.

The approach I'd use is to set sys.excepthook to be your own function that prints the exception, then waits for input. You can call the existing exception hook to actually do the printing.

import sys, traceback as tb

oldhook = sys.excepthook

def waitexcepthook(type, exception, traceback):
    oldhook(type, exception, traceback)
    input()

sys.excepthook = waitexcepthook
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文