# Tag Info

39

While I wait for better answers from some very knowledgeable people in the matter on the site, I'll write what I'm thinking... I think that most of your problems are due to lack of practice with functional thinking rather than lack of debugability itself. I think one that on the contrary, one of the advantages of programming functionally is that the state ...

32

While I agree that the debugging tools could have been better developed by now, let me just throw in a few notes and links. Function chaining (f[g[h[...]]]): I'd argue that this is a good thing. Why: Functions return expressions, which are immutable. You don't introduce as much state (or at all), as in imperative languages. This makes it easier to debug ...

18

To access the errors, you need to invoke the Front End directly from the kernel. In effect, you end up telling the kernel to tell the FE to tell the kernel to do something, so that the FE can report any errors it finds. The method I use is ClearAll[getFrontEndErrors]; SetAttributes[getFrontEndErrors, HoldAllComplete]; getFrontEndErrors[expr_] := ...

16

Here's another, reliable way: messages = {} clearMessages[] := messages = {} collectMessages[m_] := AppendTo[messages, m] InternalAddHandler["Message", collectMessages] Then do clearMessages[] 1/0; 0/0; messages InternalRemoveHandler["Message", collectMessages] Reference and details: How to abort on any message generated?

16

Two of the most common error messages that users encounter when working with parts of lists are Part::partd and Part::partw (look up Message for the error message syntax). Both of these are because the user is trying to access an invalid part of the expression (the "object" referred to in the error message), but there's a subtle difference between the two: ...

14

QuantityForm (and some other formatting functions) issues messages at typesetting instead of evaluation, and Trace is generating output that is in an unevaluated state, which QuantityForm isn't expecting. Here's a couple of similar examples: Trace[Block[{form = "LongForm"}, QuantityForm[Quantity[1, "Meters"], form]]] Trace[Block[{digits = 3}, ...

11

You could name the patterns DefFn[f_[args___], body_] := f[s : PatternSequence[args]] := WithStackFrame[{f, {s}}, body];

10

The function Shuffle is not defined. If you define it (say, replace it with RandomSample) it works. Apparently, Rotate in the latter part of the code is being applied to the output of a function that uses edgeNoise which, in turn, (because Shuffle is undefined) is producing the error message you are seeing. To replicate what is happening in a simple setting ...

9

Short Version The ultimate cause of this problem is an evaluation leak that occurs when an expression of the form MakeBoxes[StringForm[...]] is evaluated. Longer Version MakeBoxes has the attribute HoldAllComplete. This allows it to create the box representation of any expression without evaluating it. For example: MakeBoxes[1 + 1] (* RowBox[{"1", "+", ...

9

The problem here is that the Gridlines specification error message is not a kernel error message (you'll note that it is not printed with the standard Func::tag format). Instead, this warning text is generated by the front end during the rendering of the graphic. The actual generation of the gridlines values is deferred to the moment when the graphics ...

8

Following R.M's suggestion, and shamelessly lifting code from the Wizard’s fine answer there, you can use Stack[] and get the following: SetAttributes[withTaggedMsg, HoldAll] withTaggedMsg[] := Function[, InternalInheritedBlock[{MessagePacket}, Unprotect[MessagePacket]; MessagePacket[name__, BoxData[obj_, form_]] /; ! TrueQ[$tagMsg] := ... 7 Assume that you have your lists of unique resistor values (resistors) and of unique capacitor values (capacitors). For now, I generate two such lists as follows: resistors = Flatten@ Outer[ Times, PowerRange[1, 10000], {100, 110, 120, 130, 150, 160, 180, 200, 220, 240, 270, 300, 330, 360, 390, 430, 470, 510, 560, 620, 680, 750, 820, ... 7 Simply you could use$MessagePrePrint to get the "fillers" and $MessageList as you did to get the message name they belong to:$MessagePrePrint = Sow; Reap[ Module[{}, 1/0; 0^0]; $MessageList ] {{Power::infy,Power::indet},{{1/0,0^0}}} For complete control you could go low-level and intercept MessagePacket as I did for: Prepend Information to Warning ... 6 The documentation for$Messages clearly states: $Messages gives the list of files and pipes to which message output is sent. Therefore Block[{$Messages = {stream}}, ... ] is the correct syntax.

6

What you look for is the function Check which will give you the possibility to implement what you ask for in several variants, the most simple probably be this: success=Check[Import["test1.txt", "Table"];True, False] See the documentation of Check for more details...

5

In version 10 or later, we can use EvaluationData. EvaluationData[1/0; 0^0] Behind the scenes, this uses handlers, like in my first answer.

5

This error should probably be reported to WRI as a bug, most likely in ExperimentalNumericalFunction; you should not be seeing this come back up to the top level. I see no obvious reason why memory allocation should fail, as this is not really a large or difficult problem, despite the apparent complexity of the expression. However, we do not really need ...

5

You could always capture the information directly, myMessageList = {}; Internal`InheritedBlock[{Message, $InMsg = False}, Unprotect[Message]; Message[msg_, vars___] /; !$InMsg := Block[{$InMsg = True}, AppendTo[myMessageList, {msg, vars}]; Message[msg, vars] ]; (* code to run *) Module[{}, 1/0; 0^0] ]; myMessageList (* ... 5 You need a parser for the argument patterns. I wrote a simplistic one for this answer. I will reproduce it here to keep things self-contained: splitHeldSequence[Hold[seq___], f_: Hold] := List @@ Map[f, Hold[seq]]; getFunArguments[Verbatim[HoldPattern][Verbatim[Condition][f_[args___], test_]]] := getFunArguments[HoldPattern[f[args]]]; ... 4 I would do this: DefFn[f_[args___], body_] := lhs : f[args] := WithStackFrame[lhs, body]; Then make WithStackFrame HoldFirst and do de-structuring there. For example: SetAttributes[WithStackFrame, HoldFirst] WithStackFrame[f_[args___], expr_] := Print[{f, {args}}]; If for some reason this were unacceptable I would do: DefFn[f_[args___], body_] := ... 4 NDSolve has already detected the largest such intervals for you, which is why the resulting InterpolatingFunctions have restricted domains. You can use InterpolatingFunctionDomain to extract those domains. I'd do something like so Clear[x1, x2, y] eqn = {x1'[t] == -x1[t]^2 - x2[t] + y[t]^3, x2'[t] == x1[t] - x2[t] + x1[t]^2 x2[t]^2, y'[t] == x2[t]^2 ... 4 Do[ Check[{i/i, 1/(i - 5)}, Print@i], {i, -10, 10}] // Quiet 0 5 3 Check: Check[expr, failexpr] evaluates expr, and returns the result, unless messages were generated, in which case it evaluates and returns failexpr. Check[expr, failexpr, {s1::t1, s2::2, …}] checks only for the specified messages. Check[expr, failexpr, "name"] checks only for messages in the named message group. CheckAbort: ... 3 As suggested by Szabolcs, use Check nLoop = 1000; (* larger to increase likelihood of message *) Int = RandomInteger[{0, 100}, nLoop]; failCount = 0; (* initialize *) SetAttributes[recip, {Listable}]; recip[x_] := Check[1/x, failCount++;$Failed] reciprocal = recip[Int] // Quiet; (* suppress messages *) failCount (* 8 *) Use of failCount is ...

3

The problem is that you haven't defined the function getVars, which is used to define possibleC, which forms the iterator on your Do list. I tracked down the definition of getVars, getVars[p_] := Module[{L = {p}, i = 1}, While[i <= Length[L], If[Head[L[[i]]] === Symbol, i++, L[[i, 0]] = List; L = Flatten[L] ] ] ...

2

You can use Messages[foo] to get the text of any message. With that, we can proceed as follows to extract the text of the messages that were last generated: Module[{}, 1/0;0^0]; msg = \$MessageList; (* last errors *) With[{messages = ReleaseHold@ DeleteDuplicates[# /. HoldPattern@MessageName[s_, _] :> Messages@s] &}, # /. messages@#] ...

2

As suggested by @ilian , this is now better using a Nest instead of the recursive approach. Block[ {a, primes, tot}, primes = Select[Range[10^9, 10^9 + 10^3], PrimeQ]; tot = 0; Do[ tot += Nest[Mod[6 #^2 + 10 # + 3, primes[[i]]] &, 1, 10^(15) - 1]; , {i, Length@primes} ]; tot ] // AbsoluteTiming Here is an even ...

2

Finding F'x's answer a bit hard to comprehend (mainly because I'm not used to working with BoxData nor MessagePacket, but also since I didn't understand why identity was necessary there, I produced the following, which also has the advantage of producing a more useful (imho) stack, i.e. with the parameters and user defined functions printed as well, and the ...

1

Since it does not seem possible to send a Stream to a Notebook (ref: How to pipe a stream to another notebook?) I can only suggest modifying the message printing routine in some way. I propose combining these two methods: How to abort on any message generated? How to Print to the Console Evaluate this in the session: PrintToConsole[expr_] := ...

1

Another approach: a = Table[{i/i, 1/(i - 5)}, {i, -5, 5}] // Quiet {{1, -(1/10)}, {1, -(1/9)}, {1, -(1/8)}, {1, -(1/7)}, {1, -(1/ 6)}, {Indeterminate, -(1/5)}, {1, -(1/4)}, {1, -(1/3)}, {1, -(1/ 2)}, {1, -1}, {1, ComplexInfinity}} DeleteCases[a, {___, Indeterminate | ComplexInfinity, ___}] {{1, -(1/10)}, {1, -(1/9)}, {1, -(1/8)}, {1, ...

Only top voted, non community-wiki answers of a minimum length are eligible