Replace a Python exception with another one
I expect a python function I use to raise a KeyError, in case the user of my script entered a wrong value. When this happens, I want to raise a ValueError, not a KeyError. I tried to catch the KeyError, then raise a ValueError instead. I then get both. For example: def func(my_dict): return my_dict["my_key"] a = {} try: func(a) except KeyError as e: raise ValueError("Replaced exception") results in this: $ python3 except.py Traceback (most recent call last): File "except.py", line 7, in <module> func(a) File "except.py", line 2, in func return my_dict["my_key"] KeyError: 'my_key' During handli
0 upvotes
View original discussion ↗
Comments
Sign in or sign up to comment and upvote.
No comments yet.