There is good explanation available for error handling on this and similar links. Here there is information on handling inappropriate input arguments and handling unprocessed functions using $Failed. But it is much of incomplete because I want to catch error message and according to that display some specific message. For example if I get

Integrate::idiv: Integral of E^n does not converge on {1,\[Infinity]}. >>

Than I want to display some specific message in words which is possible if I can capture this message and extract from it or if Mathematica returns some error code associated.

Is there some better technique available than just using $Failed everywhere ?

  • $\begingroup$ Related: (1512), (8215) (20367) $\endgroup$
    – Mr.Wizard
    Jul 27, 2013 at 14:10
  • $\begingroup$ @Mr.Wizard: Thanks for closely related link, I am going through it now. $\endgroup$ Jul 27, 2013 at 14:19
  • 2
    $\begingroup$ I added two more links in the comment above. I think Internal`AddHandler["Message", . . .] may be exactly what you're looking for. $\endgroup$
    – Mr.Wizard
    Jul 27, 2013 at 14:33
  • 1
    $\begingroup$ this link is also related $\endgroup$
    – Murta
    Jul 27, 2013 at 22:08
  • 2
    $\begingroup$ I personally use exceptions where I put enough information in the arguments of Throw to find out which error it was. No time right now do decribe this in more detail, but it is a logical extension of the exception-based method I described in the link you cited. You can also look at the source code of RLink to see this applied in practice. $\endgroup$ Jul 28, 2013 at 12:09

1 Answer 1


This is an attempt to answer my question. Using the links suggested in comments I came up with following steps. Let there be some sample function 'sam'.

$MessagePrePrint = Sow;
    Sam[a_, b_] := 
     Reap[Module[{}, {1/0, LinearSolve[{{a}}, {{b}}]}]; $MessageList]

Making use of Mathlink,

    link = LinkLaunch[First[$CommandLine] <> " -mathlink"]
    LinkWrite[link, EnterTextPacket[Sam[0, 1]]]
    t = LinkRead[link]

This gives


Collecting information out of above,

l = Level[t, 3][[1]]

returns= {Power::infy,LinearSolve::nosol}

Using replace ,ty=l/.{Power::infy->1,LinearSolve::nosol->2}

Now a separate function can be written to pass this list to and place custom messages.

Pr[a_] := Module[{}, { a, "->",
   If[a === 1, "No solution available",
    If[a === 2, "Illegal division", If[a === 3, "Sample error "]]]}]

Calling it using Table[Pr[ty[[i]]], {i, 1, Length[ty]}]

gives {{1, "->", "No solution available"}, {2, "->", "Illegal division"}}

Actually @Mr.Wizard's answer on one of the link, answers with lot less hustle but this way I learned one more thing. If is not really a professional solution and bit childish lacking professional approach.

I would request if someone would like to amend/update this question, please go ahead. Suggestion are most welcome.

  • $\begingroup$ One thing I wanted to point out is that if Reap/Sow is not used in that case only the last error is captured by EnterTextPacket. Rest above it are ignored. As far as replacement rules are concerned they can be implemented as ruleset. $\endgroup$ Jul 30, 2013 at 7:06
  • $\begingroup$ One more way I found out to collect messages is used Trace[expr,Message[ ___ ]]. It is in documentation. $\endgroup$ Aug 4, 2013 at 9:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.