You raised another exception in the exception handler, which was swallowed because there is a finally handler that returns from the function.
You cannot just concatenate the exception object and string, so an additional TypeError is added, and assignment to the dictionary is never achieved.
Converting an exception to a string first works:
>>> def test(): ... temp = dict() ... try: ... raise ValueError("something") ... except Exception as error: ... print("error is :{}".format(error)) ... temp['except'] = "something" + str(error) ... finally: ... return temp ... >>> test() error is :something {'except': 'somethingsomething'}
In the try documentation:
If finally present, it indicates a "cleanup handler." The try clause is executed, including any except and else . If an exception occurs in any of the sentences and is not handled, the exception is temporarily saved . The finally clause is executed. If there is a stored exception, it will be raised again at the end of the finally clause. If the finally clause raises another exception, the saved exception is set as the context of the new exception. If the finally clause executes a return or break return , the stored exception is thrown [.]
(My bold accent).
source share