Python 我在异常处理中有一个异常,这是不同的?
在最后一行中,当使用alias打印Python 我在异常处理中有一个异常,这是不同的?,python,Python,在最后一行中,当使用alias打印i时,消息运行正常 except insufficient as i : print("exception is caught",i.msg) 但是当我这么做的时候 except insufficient : print("exception is caught",insufficient.msg) 这是个错误……为什么 ```class insufficient(ZeroDivisionError): def __init__(se
i
时,消息运行正常
except insufficient as i :
print("exception is caught",i.msg)
但是当我这么做的时候
except insufficient :
print("exception is caught",insufficient.msg)
这是个错误……为什么
```class insufficient(ZeroDivisionError):
def __init__(self,arg):
self.msg=arg
balance=5000
w=int(input("enter a number"))
try:
if w>5000:
raise insufficient("balance in the account is insufficient")
balance=balance-w
print("no exception and balance is=",balance)
except insufficient :
print("exception is caught",insufficient.msg)```
错误日志:-
Traceback (most recent call last):
File "C:\Users\ahmod\AppData\Local\Programs\Python\Python37-32\hello.py", line 8, in <module>
raise insufficient("balance in the account is insufficient")
insufficient: balance in the account is insufficient
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\ahmod\AppData\Local\Programs\Python\Python37-32\hello.py", line 12, in <module>
print("exception is caught",insufficient.msg)
AttributeError: type object 'insufficient' has no attribute 'msg'
回溯(最近一次呼叫最后一次):
文件“C:\Users\ahmod\AppData\Local\Programs\Python\Python37-32\hello.py”,第8行,在
募集不足(“账户余额不足”)
不足:账户余额不足
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“C:\Users\ahmod\AppData\Local\Programs\Python\Python37-32\hello.py”,第12行,在
打印(“捕获到异常”,不足.msg)
AttributeError:类型对象“不足”没有属性“msg”
当您执行以下操作时:
except insufficient as i:
try
块中的代码抛出的任何异常
都被分配给变量i
,并在except
块中可用。因此,如果您的try
块抛出不足的异常,则该异常被分配给i
。因此,您可以参考i.msg
但是由于msg
是类uncipled
中的对象属性,uncipled.msg
的计算结果为空
最后:
except insufficient:
不会使抛出的对象在块中可用,但
块除外<代码>不足
这里只是一个类型标识符。因此,您无法访问msg
属性 当您这样做时:
except insufficient as i:
try
块中的代码抛出的任何异常
都被分配给变量i
,并在except
块中可用。因此,如果您的try
块抛出不足的异常,则该异常被分配给i
。因此,您可以参考i.msg
但是由于msg
是类uncipled
中的对象属性,uncipled.msg
的计算结果为空
最后:
except insufficient:
不会使抛出的对象在块中可用,但
块除外<代码>不足
这里只是一个类型标识符。因此,您无法访问msg
属性 除了作为e的不足之外
然后执行e.msg。此外,我相信您可以使用super()。\uuuu init\uuuu(),这样您就可以使用常规的异常,但作为e
e.args[0]
msg
不是类属性,这就是为什么不创建实例就无法访问它。除了不足够的e
之外,请执行e.msg。此外,我相信您可以使用super()。\uuuu init\uuuu(),这样您就可以使用常规的异常,但作为e
e.args[0]
msg
不是类属性,这就是为什么不创建实例就无法访问它的原因。但是如果我写得不够充分,就像我只是一个不够充分的类的另一个名称一样??那么我和班上有什么不同呢insufficient@AbdaalAhmad:i
不仅仅是类的另一个名称。您可能将导入规则或与
语句混在一起i
是捕获的不足
类实例的名称。不足
是该类的名称i
是不足类的一个实例。@user2357112请您参考这个概念的一些源链接,以便我能更好地理解这个概念,但如果我写得不够,因为我只是不足类的另一个名称??那么我和班上有什么不同呢insufficient@AbdaalAhmad:i
不仅仅是类的另一个名称。您可能将导入规则或与
语句混在一起i
是捕获的不足
类实例的名称。不足
是该类的名称i
是Unlimited
类的一个实例。@user2357112您可以参考此概念的一些源链接,以便我更好地理解此概念吗