[Debug] Restoring error handler before assertions
minor #11959 [Debug] Restoring error handler before assertions (fejese)

This PR was merged into the 2.5 branch.

Discussion
----------

[Debug] Restoring error handler before assertions

[Debug] Improving unit test

As a follow up of @stof's comment on #11952 the reset of error handlers been moved up before the assertions to clean up even in case of a failure.

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |

Commits
-------

51fa3ef [Debug] Restoring error handler before assertions