# Exception/Error handling using returns codes

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 ?

-
Related: (1512), (8215) (20367) – Mr.Wizard Jul 27 '13 at 14:10
@Mr.Wizard: Thanks for closely related link, I am going through it now. – Rorschach Jul 27 '13 at 14:19
I added two more links in the comment above. I think InternalAddHandler["Message", . . .] may be exactly what you're looking for. – Mr.Wizard Jul 27 '13 at 14:33
this link is also related – Murta Jul 27 '13 at 22:08
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. – Leonid Shifrin Jul 28 '13 at 12:09

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]


    link = LinkLaunch[First[\$CommandLine] <> " -mathlink"]


This gives

ReturnPacket[EnterTextPacket[{{Power::infy,LinearSolve::nosol},{{1/0}}}]]


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.

-
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. – Rorschach Jul 30 '13 at 7:06
One more way I found out to collect messages is used Trace[expr,Message[ ___ ]]. It is in documentation. – Rorschach Aug 4 '13 at 9:03