When I try to handle the exception by using the custom exception class , do I need to use alias?

Viewed 9
class InsufficientBalance(ZeroDivisionError):
    def __init__(self,text):
        self.text=text
balance=5000
try:
    AmtToBeWithdrawn=int(input("Enter the amount you want to withdraw"))
    if AmtToBeWithdrawn>balance:
        raise InsufficientBalance("Bhosdike aukat me rahkar amount dal")
except InsufficientBalance as i:
    print(i.text)
else:
    balance=balance-AmtToBeWithdrawn
    print("Withdrawal Successfull")
finally:
    print("Remaining balance on the account",balance)

When we use the as keyword in the above code , does the as keyword create the object of the InsufficientBalance class? because if that does not create the object then , how is it possible to access the instance variable text? When I just write

except InsufficientBalance as i:
    print(i.text)
it gives the exception 

Traceback (most recent call last):
     File "C:/Users/HP/Desktop/Fullstack dev SR/Python/ASS27.py", line 103, in <module>
     print(InsufficientBalance.text)
AttributeError: type object 'InsufficientBalance' has no attribute 'text'

but when I write

except InsufficientBalance as i:
    print(i.text)

the code does not produce any exception , why is it so? Thanks in advance for replying.

0 Answers
Related