Note that the error numbers should not be hard-coded in subsequent calls to error/2 etc., as the error numbers are arranged at run time and may be changed between releases.
Success:
      define_error("my first error message",N).  (gives N=340).
      define_error("my second error message",N). (gives N=341).
      [eclipse]: [user].
       :- define_error("1st arg should be number",N),
          setval(usererror, N).
       do(N,Res) :-(number(N) ->
              Res is sqrt(N)
              ;
              getval(usererror, Err),
              error(Err, do(N,Res))).
       user compiled 232 bytes in 0.00 seconds
      yes.
      [eclipse]: do(4,2.0).
      yes.
      [eclipse]: do(four,2.0).
      1st arg should be number in do(four, 2.0)
Error:
      define_error(M,N).                    (Error 4).
      define_error(atom,N).                 (Error 5).
      define_error("incorrect module",340). (Error 5).