[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        |