4

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 ?

Pankaj Sejwal
  • 2,063
  • 14
  • 23
  • @Mr.Wizard: Thanks for closely related link, I am going through it now. – Pankaj Sejwal Jul 27 '13 at 14:19
  • 2
    I added two more links in the comment above. I think Internal`AddHandler["Message", . . .] may be exactly what you're looking for. – Mr.Wizard Jul 27 '13 at 14:33
  • 1
    this link is also related – Murta Jul 27 '13 at 22:08
  • Blackbird, have you gone through those links yet, especially 20367? If you have and a question remains please clarify it and I'll attempt to answer. If not I may close this question as a duplicate, or if you prefer you can summarize your solution in an answer to your own question. – Mr.Wizard Jul 28 '13 at 08:10
  • @Mr.Wizard: Please give me time for 3-4 days..I have some office work pending. – Pankaj Sejwal Jul 28 '13 at 08:19
  • 2
    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
  • @Mr.Wizard:can you please contribute an answer here as you were offering to. – Pankaj Sejwal Jul 30 '13 at 08:32
  • Blackbird, I just found your request again; I know I read it but I forgot. Sorry. Do you still need help with this? – Mr.Wizard Aug 29 '13 at 07:43
  • @Mr.Wizard: Actually I was exploring error code handling, than I got this question. But it will be great if you could include something worthy to make this post a learning place for everyone. – Pankaj Sejwal Aug 29 '13 at 07:46

1 Answers1

2

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

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.

Pankaj Sejwal
  • 2,063
  • 14
  • 23
  • 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. – Pankaj Sejwal Jul 30 '13 at 07:06
  • One more way I found out to collect messages is used Trace[expr,Message[ ___ ]]. It is in documentation. – Pankaj Sejwal Aug 04 '13 at 09:03