2024-04-25 13:21:35 发布
网友
我见过有人这样做,但我看不出他们之间的区别:
raise Exception('This is the error')
以及
raise 'This is the error'
我应该用哪一个?你知道吗
两个都不要用。第一个是语法错误:
>>> raise Exception "This is an error" File "<stdin>", line 1 raise Exception "This is an error" ^ SyntaxError: invalid syntax
第二个是类型错误(不能“提升”一个str值):
str
>>> raise "this" Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: exceptions must derive from BaseException
正确的形式是使用错误消息作为参数调用异常类型:
raise Exception("this is the error")
在所需异常不需要参数的情况下,引发Exception类型本身相当于引发不带参数创建的实例。你知道吗
Exception
raise Exception # equivalent to raise Exception()
两个都不要用。第一个是语法错误:
第二个是类型错误(不能“提升”一个
str
值):正确的形式是使用错误消息作为参数调用异常类型:
在所需异常不需要参数的情况下,引发
Exception
类型本身相当于引发不带参数创建的实例。你知道吗相关问题 更多 >
编程相关推荐