GH-98897: fix memory leak if `math.dist` raises exception (GH-98898)

(cherry picked from commit ab575050709e2b313ca9a9585f09b6f4b0560318)

Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
3 files changed