简体   繁体   English

如何创建一个用户定义的异常,如果不处理,系统会退出? (异常类似乎不够。)

[英]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.

相关问题 为什么我的用户定义的异常没有得到正确处理? - Why isn't my user defined exception being handled properly? 在python 3.6中,如何捕获异常并引发异常以便以后处理? - In python 3.6, how do I catch an exception and raise an exception to be handled later? 如何创建 class 的 static 实例,这会导致构造函数中出现异常? - How to create a static instance of a class when it would lead to an exception in the constructor? 如何使用 python zeep 进行重试处理? 我正在使用请求重试 session,但未处理异常 - How do I get retry handling with python zeep? I'm using a requests retry session, but the exception is not handled Python在类声明时引发用户定义的异常 - Python Throwing User Defined Exception at Class Declaration 在Python中的用户定义类中将用户定义的异常子类化 - Subclassing a user-defined exception in a user-defined class in Python 如何对空白进行例外处理,使其不会 output a 0? - How do I make an exception for a blank space so it would not output a 0? 用户定义的例外疑问 - User defined Exception doubt Python:如何捕获和处理用户定义的异常? - Python: How to catch and handle a user defined exception? 如何确定特定异常类的模块定义 - How to Determine The Module a Particular Exception Class is Defined In
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM