Fix error handling for the exist function