[英]How do I create a user defined exception that would system exit if not handled? (Exception class seems insufficient.)
The python Exception
class is supposed to be used for all user defined exceptions. python
Exception
类应该用于所有用户定义的异常。 Documentation also says it is used by all non-system-exiting builtin exceptions.文档还说它被所有非系统退出的内置异常使用。 I need to create a user defined exception that will system exit if not handled.
我需要创建一个用户定义的异常,如果不处理它将系统退出。 In quick tests, calling a method (while not using
try
... catch
) that raises my user defined exception doesn't cause an exit.在快速测试中,调用引发我的用户定义异常的方法(而不使用
try
... catch
)不会导致退出。 In debugging, I can see that the exception is 'received' by the python interpreter, but it doesn't cause an exit.在调试中,我可以看到 Python 解释器“接收到”异常,但它不会导致退出。
Are you sure?你确定吗? Exceptions do normally cause the program to exit.
异常通常会导致程序退出。 The following never reaches the
print
statement.以下永远不会到达
print
语句。
class MyError(Exception):
pass
raise MyError
print("survived")
If you see different results, it must be because you are doing something else.如果您看到不同的结果,那一定是因为您正在做其他事情。 Add your code to your question if you still can't see what you are doing wrong.
如果您仍然看不到您做错了什么,请将您的代码添加到您的问题中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.