“`”
<pre><code># 触发异常
def temp_convert(var):
try:
return int(var)
except ValueError as Argument:
print (""参数没有包含数字%s""%Argument)
# 调用函数
temp_convert(""xyz"")
# 以10为基数的int()的无效文字:“xyz”
—————————————————————————-
# raise语法
#raise [Exception [, args [, traceback]]]
# 语句中 Exception 是异常的类型,args 是自已提供的异常参数。
class Networkerror(RuntimeError):
def __init__(self, arg):
self.args = arg
try:
raise Networkerror(""Bad hostname"")
except Networkerror as e:
print(e.args)
</code></pre>
<pre><code> "“`
Was this helpful?
0 /
0