Handle malloc failure by throwing exceptions.