gpt4 book ai didi

python - 如何在 Python 3.3 中引发异常

转载 作者:行者123 更新时间:2023-11-28 20:45:48 24 4
gpt4 key购买 nike

我目前正在阅读 tutorialspoint 的 python 教程中的教程。但它是 Python 2 的教程,而不是我现在拥有的 Python 3.3。好吧,我设法在互联网上搜索并发现了一些变化。但是这个很难。

因此,在tutorialspoint中引发异常的python源代码是:

def functionName( level ):
if level < 1:
raise "Invalid level!", level
# The code below to this would not be executed
# if we raise the exception

但是如果我输入

raise "Invalid level!", level  

它说语法错误。所以,我想知道如何在 Python 3.3 中引发异常。

最佳答案

语法是:

raise Exception("Invalid level! " + level)

我真的建议您阅读 Python docs.

关于python - 如何在 Python 3.3 中引发异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22446895/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com