The error checking is suspicious it should check for length == 0 and get correct exception.