Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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 ?

share|improve this question
    
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
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
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
show 5 more comments

1 Answer

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.

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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