Try/Except to get a key with "as" keyword and without

I’m really stuck, and cannot derive fir myself what is the difference:

caffeine_level = {"espresso": 64, "chai": 40, "decaf": 0, "drip": 120}

try:
  print(caffeine_level["matcha"])
except KeyError:
  print(KeyError)


<class 'KeyError'>

and this one:

caffeine_level = {"espresso": 64, "chai": 40, "decaf": 0, "drip": 120}

try:
  print(caffeine_level["matcha"])
except KeyError as k:
  print(k)
  print(type(k))

'matcha'
<class 'KeyError'>

I was thinking “as” was standing to create an alias
But now I can see 3 different behaviour:

  1. import datetime as dt,
  2. with open(“jsonfile.json”,“r”) as f
  3. except KeyError as k:

Please help.

Nothing really exceptional about this. as lets us give an alias to the default error object.

Compare this to the import statement…

from random import randrange as rge

print (rge(10))    # an integer from 0 to 9 inclusive

Thanks a lot for reply.

But it does not answer the question why exception has not been printed in first place if this is just an alias.

except KeyError:
  print(KeyError)


<class 'KeyError'>

comapared to this:

except KeyError as k:
  print(k)
  print(type(k))

'matcha'
<class 'KeyError'>

https://wiki.python.org/moin/HandlingExceptions

See if that doesn’t answer your question.

There’s no operator named as, it isn’t a thing, it’s text.
Similarly, the “in” in a for-loop is not the “in” operator.

Thanks for the reply. Main focus in the article is best practice for handling exceptions.

While my question was: What is happening behind the curtain ?

except KeyError:
vs
except KeyError as k:

I would expect those two lines to be equivalent, but reality is different. I’m only starting my journey with Python and have a very shallow knowledge atm. I did quite a bit of googling but haven’t found anything apart from empirical evidence “do this and you get there, or do this and you get that” . This is not my cup of tea, I’m really passionate how things works from end to end.

There’s no reason to expect them to be entirely equivalent. One of them mentions a variable and the other does not, that’s a pretty big difference, doesn’t that suggest that one of them does something with k, and the other does not? You could start with what the language promises to do with a try-statement, but that’s not in any way behind the scenes, that’s the basic promise that you leverage.
https://docs.python.org/3/reference/compound_stmts.html#the-try-statement
And yes, it is in the form of do-this-get-that, it’s an agreement, a contract. It isn’t supposed to say how it’s done, only what’s promised. It is for the programmer to leverage those promises, and for the language implementation to make it happen in any way that satisfies those promises.

1 Like

Thanks a lot for reply. You’re right…

Even in link they say

exception must be assigned to a different name to be able to refer to it after the except clause.

Everything else my imagination.
I was thinking there was some magic brewing with instantiation of object for KeyError class.

If this is the way programmer did it, I can buy it. In case of C/C++ it could have been different story.