You can break out of a function with an error by using the assert command, which takes a boolean as an argument. If the boolean is false, then the function will return with an error. For example, if you define a function
sqofpos(x) := {assert(x > 0); return x^2;}
then if you enter
you will get 16, but
will return an error, since -4 > 0 is false.