Adding error check while typecasting