1

in the last line when i was printed the message by using alias it was running fine .for example

except insufficient as i :
    print("exception is caught",i.msg)

but when I did this

except insufficient :
    print("exception is caught",insufficient.msg)

it was an error ..why???

```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)```

Error Log:-

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'
  • `except insufficient as e` then do e.msg. Also I believe you can use the super().__init__() so that you can use the conventional `except Exception as e` `e.args[0]`. `msg` is not a class property that's why you can't reach it without creating an instance. – Buckeye14Guy May 26 '19 at 18:30
  • [super](https://stackoverflow.com/questions/576169/understanding-python-super-with-init-methods) – Buckeye14Guy May 26 '19 at 18:32

1 Answers1

4

When you do:

except insufficient as i:

Whatever Exception is thrown by the code in the try block is assigned to the variable i and is made available in the except block. So if your try block throws an insufficient exception then that exception is assigned to i. Hence you can refer to i.msg.

But since msg is a object attribute in the class insufficient, insufficient.msg evaluates to nothing.

Lastly:

except insufficient:

Does not make the thrown object available in the except block. insufficient here is merely a type identifier. So you can't access the msg attribute.

rdas
  • 20,604
  • 6
  • 33
  • 46
  • but if I write it as insufficient as i is just another name of an insufficient class ?? so how i is different from class insufficient – Abdaal Ahmad May 26 '19 at 18:41
  • 1
    @AbdaalAhmad: `i` is not just another name for the `insufficient` class. You may be mixing things up with the rules for imports or `with` statements. `i` is a name for the caught *instance* of the `insufficient` class. – user2357112 May 26 '19 at 18:45
  • 1
    `insuffiecient` is the name of the class. `i` is an instance of the `insufficient` class. – rdas May 26 '19 at 18:45
  • @user2357112 could u please refer some source link of this concept so that i can understand this concept better – Abdaal Ahmad May 26 '19 at 19:00