如果你用Ctrl + C停止一个python脚本,它会执行任何finally块,还是它会在字面上停止脚本的位置?
我发现自己处于类似于下面代码的情况。
我有一个 try 块,并且正在测试两个不同变量的值。如果其中之一不正确,我想引发 ValueError 异常,但我想根据发生的情况以不同方式处理它们。
fingers = 8
thumbs = 3
try:
if fingers == 8:
print("8 Is the usual amount of fingers")
else:
raise ValueError("That's a strange amount of fingers to have")
if thumbs == 2:
print("You have the usual amount of thumbs")
else:
raise ValueError("That's a strange amount of thumbs to have")
except ValueError as e1:
print("You had the wrong amount of fingers - let me perform surgery and fix that for you")
fingers = 8
except …Run Code Online (Sandbox Code Playgroud) 我正在编写一个骰子滚动程序,它有两个参数传递给主程序,骰子有多少面以及你想扔的次数。如果传递的参数少于或多于两个,我想抛出异常。我该怎么做呢?
我找到了这个。但我不确定如何使用它?当然,我必须以某种方式指定在抛出异常之前预期的参数数量?