math: add exception handling functionality