fixes #4929
improve error handling
Merge branch 'master' into issue_4929
convert handle check into assert
Merge pull request #8594 from amartgon/issue_4929

fixes #4929