I am trying to set up a Mathematica script so that it runs from the command line in the manner that a sane person would expect. I have the following at the start of the file:
#!/usr/local/bin/MathKernel -script
which lets me run my script from the console like so
./<my_code>.m <arguments>
(after chmod-ing it) and I retrieve the various arguments like so:
args = Join[$CommandLine[[4;;]],Table[{},{i,1,10}]];
(I have 4 arguments; I forget why the Table is 10 slots long, I must have had a reason for that...) and then I can use them like
somevariable = args[[1]];
and so on. Another innovation I learned of (from this question: How to abort on any message generated?) is the following:
messageHandler = If[Last[#], Print["An error has occurred in '<my_code>.m'; aborting evaluation..."] Exit[1];]&
Internal`AddHandler["Message", messageHandler]
which I use to make the script abort when something goes wrong, to prevent it going completely haywire as Mathematica is wont to do.
So now to the next step; when the script dies due to some problem occurring (as picked up by the above messageHandler), I would like to know what line number of my input file caused the problem. I found the following comment on an old mailing list (http://forums.wolfram.com/mathgroup/archive/2008/Jul/msg00091.html)
Aaron Fude wrote:
Hi,
Mathematica is very good at reporting that you've made an error, but it doesn't tell you where the error occurred. Is there a way to obtain the "stacktrace"?
Many thank in advance!
Aaron
to which the only reply was
Hi Aaron,
if you write your code in a *.m file and read it in, Mathematica will report the error line number.
Daniel
So ok it sounds like Mathematica can do it somehow, but it sure doesn't do it by default when I run my script from the command line rather than import it into a notebook. So is there a way to "switch on" such behaviour? I also found the following question on the topic: Finding a Specific Line in a Package but I don't quite see how to apply that to my scenario, and it seems like it might be overkill anyway.
Lots of hassle to get sensible batch job behaviour! I have further issues trying to get it to print output in a readable/parsable one-line format (rather than all the stupid multi-line formatting that it does by default for fractions etc; see Set form of all output to InputForm) but perhaps that is for another question...
edit: Ok, it seems like sometimes Mathematica does decide to give me a line number for the error, e.g.:
Syntax::sntx: Invalid syntax in or before "If[Count[validiso,isotope]==0], " (line 44 of "./recoil_generator.m").
which is nice, but it doesn't do it all the time.
Message
because there's no direct link between the in-memory program and the source file. In most of the cases there is no source file at all. Definitions can be issued from the command line, or created completely programmatically. Looking at the title of the question my first reaction would be to say, "inherently impossible". But you are not asking only for line numbers but also for some sort of stack trace, which should be possible. Therefore I suggest re-titling the question. $\endgroup$