在使用别名打印i
消息的最后一行中,.for示例运行得很好
except insufficient as i :
print("exception is caught",i.msg)
但是当我这么做的时候
except insufficient :
print("exception is caught",insufficient.msg)
这是一个错误,..why?
```class insufficient(ZeroDivisionError):
def __init__(self,arg):
self.msg=arg
balance=5000
W=int(input(“输入数字”))
尝试:
if w>5000:
raise insufficient("balance in the account is insufficient")
balance=balance-w
print("no exception and balance is=",balance)
除了不足:
print("exception is caught",insufficient.msg)```
Error Log:-
```javascript
回溯(最近一次调用):
文件"C:\Users\ahmod\AppData\Local\Programs\Python\Python37-32\hello.py",第8行,在
raise insufficient("balance in the account is insufficient")
不足:账户余额不足
在处理上述异常的过程中,发生了另一个异常:
回溯(最近一次调用):
文件"C:\Users\ahmod\AppData\Local\Programs\Python\Python37-32\hello.py",第12行,在
print("exception is caught",insufficient.msg)
AttributeError:类型对象“”不足“”没有属性“”msg“”
发布于 2019-05-27 02:31:15
当你这样做的时候:
except insufficient as i:
try
块中的代码抛出的任何Exception
都被赋值给变量i
,并在except
块中可用。因此,如果你的try
代码块抛出了一个insufficient
异常,那么这个异常就会被分配给i
。因此,您可以参考i.msg
。
但由于msg
是类insufficient
中的对象属性,因此insufficient.msg
的计算结果为nothing。
最后:
except insufficient:
不会使抛出的对象在except
块中可用。这里的insufficient
仅仅是一个类型标识符。所以你不能访问msg
属性。
https://stackoverflow.com/questions/56316240
复制相似问题