Instead of having several h.isValid() checks in this function. It would be safer to put the whole function implementation in a try / catch. The exception to catch is ```invalid_handle```.