简体   繁体   English

如何在异常发生后立即停止执行 function? [Python]

[英]How to stop the execution of a function right after the exception happened? [Python]

Let's say I have following code:假设我有以下代码:

def main():
  try:
    int('string_for_ValueError')
  except ValueError:
    print('How to stop further execution right here?')
  print('Executed')

main()

As you can see, print('Executed') line will be executed every time no matter what.如您所见,无论如何,每次都会print('Executed')行。 My goal is to stop the execution of the current function right after except ValueError is caught.我的目标是在异常except ValueError被捕获后立即停止当前 function 的执行。

So, the question is - how to do that?所以,问题是 - 如何做到这一点?

UPDATE更新

This function is a part of the multithreaded algorithm.这个 function 是多线程算法的一部分。 So, if this function cannot be executed because of except ValueError - this function should stop and does not return anything.所以,如果这个 function 因为except ValueError而不能执行 - 这个 function 应该停止并且不返回任何东西。 But other threads should work after that.但是其他线程应该在那之后工作。

You can simple raise an error with a message to stop execution.您可以简单地通过消息引发错误以停止执行。 If you are using try except you basically want the program not to stop from execution when errors found.如果您使用 try ,除非您基本上希望程序在发现错误时不要停止执行。 If you really want to do that, you can do it this way, but you can do it several ways.如果你真的想这样做,你可以这样做,但你可以通过多种方式做到这一点。 My question is why you are catching this error if you want to stop the program?我的问题是,如果您想停止程序,为什么会遇到此错误?

EDIT AFTER YOUR UPDATE: You can add return to your function.更新后编辑:您可以将返回添加到 function。

def main():
  try:
    int('string_for_ValueError')
  except ValueError:
    print('How to stop further execution right here?')
    return "to something"
  print('this line not executed because function sees the return')

main()

You can quit a Python script using exit("Failure") where the string is an optional message.您可以使用exit("Failure")退出 Python 脚本,其中字符串是可选消息。

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

相关问题 Python:如何在捕获到异常后停止执行? - Python : How to stop execution after exception has been caught? Python:异常停止后,如何继续在try语句内执行 - Python : How to continue execution inside a try statement after an exception stop it 出现异常/错误时如何不停止python中其他函数的执行 - How not to stop the execution of other function in python in case of Exception/Error 发生错误后,使用raise函数停止在Python中进一步执行代码 - Using raise function to stop execution of further code in python after an error Python:停止执行 function - Python: Stop function from execution 如何在函数内执行Python函数而不是停止执行 - How to execute a Python function within a function and not stop the execution 在Python`unittest`中,如何在try中发生异常/警告后返回一个值-除外? - In Python `unittest`, how can I return a value after exception/warning happened in try - except? Python 3-打印回溯并在工作程序异常时停止执行 - Python 3 - print traceback and stop execution on worker exception Plotly / Dash - Python 如何在一段时间后停止执行? - Plotly / Dash - Python how to stop execution after time? 如何停止从外部执行Python函数? - How can I stop the execution of a Python function from outside of it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM