The precedence of raise
and handle
is a bit weird in SML. What you have written groups as
raise ((Negative "...") handle Negative msg => 0)
Consequently, you need to add parentheses around the if
to get the right meaning.
On the other hand, I don't understand why you raise an exception just to catch it right away. Why not simply return 0 in the else
branch?
Edit: If you want to print something and then return a result, use the semicolon operator:
(print "error"; 0)
However, I would strongly advise against doing that inside the factorial function. It's better to keep I/O and error handling separate from basic computational logic.