Added basic exception handeling to invariant calculation