1

I would like to print a warning/error message from my own module. Of course I could use Print["WARNING: my warning"] but in all the output this would be barely visible. Ideally my user defined message would appear in the same style as those produced by Mathematica built-in functions.

Michael E2
  • 235,386
  • 17
  • 334
  • 747
highsciguy
  • 1,700
  • 1
  • 16
  • 23

1 Answers1

1

You can do this using Mathematica's built-in message system; also, I'd suggest looking through Mathematica's package development documentation and tutorials, as these commonly go hand-in-hand; here is one starting place.

As for the specific question, here is an example:

MyFunction::argerr = "Bad argument given to MyFunction: `1`";
MyFunction[x_Integer] := Mod[x^2, x+1];
MyFunction[x_] := (
  Message[MyFunction::argerr, x];
  $Failed);

MyFunction[10]

1

MyFunction["abc"]

MyFunction::argerr: Bad argument given to MyFunction: abc
$Failed

nben
  • 2,148
  • 9
  • 20