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

问题描述 投票:0回答:1

假设我有以下代码:

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

main()

如您所见,无论如何,每次都会执行print('Executed')行。我的目标是在捕获except ValueError之后立即停止当前函数的执行。

所以,问题是-怎么做?

python python-3.x function exception execution
1个回答
0
投票

您可以使用exit("Failure")退出Python脚本,其中字符串是可选消息。

© www.soinside.com 2019 - 2024. All rights reserved.