fix hg error handling, add exception handler, fix index out of bounds exception