Episode #125 of the Stack Overflow podcast is here. We talk Tilde Club and mechanical keyboards. Listen now
44

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 ...


34

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 ...


27

Is there a possibility to disable stack tracing, but keep messages? Internal`$MessageMenu = False reverts back to the old messages. Seems to do the trick and prevent the leak from my testing.


24

Analysis current as of Mathematica version 11.0.1 and 11.1.0. We can disable the Show Stack Trace item in the new message menu as follows: MessageMenu`$PruneStack; MessageMenu`Dump`$IncludeStack = False; The reference to MessageMenu`$PruneStack is there to ensure that the message menu packages have been autoloaded (otherwise our setting will be lost ...


21

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_] := Block[{...


21

Update See here for a documented way to do the very same thing in v10.0 or later. This method will only catch those messages which would actually get printed, not those which are Quieted or turned Off. We can use handlers: messages = {} clearMessages[] := messages = {} collectMessages[m_] := AppendTo[messages, m] Internal`AddHandler["Message", ...


19

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: ...


18

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, meaning that only those messages will be recorded which would get printed.


15

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}, NumberForm[N[...


15

For the case where the height function is "Count", we can use the formula from the linked page in a custom ChartElementFunction with the sample size (Length[data]) passed as metadata: ceF[d_: .2, nsd_: 3, color_: Automatic][cedf_: "Rectangle"] := Module[{e = nsd /2 Sqrt[#[[2, 2]] (1 - #[[2, 2]]/ #3[[1]])]}, {ChartElementData[cedf][##], Thick, ...


14

What I personally usually do is to still use $Failed / exceptions when a returned $Failed / thrown exception can't be used constructively when handled / caught, other than issuing the right message. In some cases, however, the application logic requires to do more than that. In particular, certain data associated with the state right before the failure may ...


12

This has been partially answered before, so here I will highlight some of its evolution since that previous answer. VerificationTest and its MUnit` antecedents do not have a mechanism for introducing new error types, so we need a function to do that for us. For this, I use a function called checkGraphicsRendering (outlined at the end) with the general use ...


11

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


11

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 ...


10

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", "+", ...


10

Consider any numerical integration method $I^*(f,a,b)$ that approximates the exact integral $I$ of a function $f$ over an interval $[a,b]$. It will be implemented by a computation represented by, say, I[f[x], {x, a, b}]. Conceiving an NIntegrate[] command in this way breaks the error into two independent components. The first is the error of the method $I^* -...


10

There are a few reasonable ways. I'll illustrate with an example of Newton iterations for square roots, take from this MathGroup post r[x_, n_] := x - (x^2 - n)/(2*x) x = 1.0`20; two = 2.0`20; First we run it with the usual arithmetic. Table[x = r[x, two], {30}] (* Out[680]= {1.4142135623730950488, 1.4142135623730950488, \ 1.414213562373095049, 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 ...


9

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 ...


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[, Internal`InheritedBlock[{MessagePacket}, Unprotect[MessagePacket]; MessagePacket[name__, BoxData[obj_, form_]] /; ! TrueQ[$tagMsg] := Block[{$...


8

The following method will capture all messages, regardless of whether they are Quieted or turned Off. Messages that are turned off will be wrapped in $Off. You could always capture the information directly, myMessageList = {}; Internal`InheritedBlock[{Message, $InMsg = False}, Unprotect[Message]; Message[msg_, vars___] /; ! $InMsg := Block[{$InMsg = ...


8

That would mean to remove the Listable attribute from Plus. You could do it as follows, but I would not recommend that. Unprotect[Plus]; ClearAttributes[Plus, Listable]; Plus[_?MatrixQ, __?(! MatrixQ[#] &)] := $Failed Now, you have a = IdentityMatrix[3]; Plus[a, a] Plus[a, 1] {{2, 0, 0}, {0, 2, 0}, {0, 0, 2}} $Failed Really, you should ...


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

I think that that the key feature is to use the MaxExtraConditions option for the Solve command. In elaborate answer of Artes in here, a very very nice presentation is referred. It is entitled as Getting the Most from Algebraic Solvers in Mathematica by Adam Strzeboński. You can download the .cdf file of the presentation which is really helpful. Slide $10$ ...


7

I think Internal`WithLocalSettings is the tool you want to use. The syntax is: Internal`WithLocalSettings[ preliminaryCode, (* can't be aborted *) body, (* can be aborted *) postprocessCode (* can't be aborted *) ] Suppose your code looks like: code[] := (a=1; b=2; Pause[Infinity]; c=3) and you're interested in the values of a, b and c. Then: ...


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...


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

This does what I think you're after, fiddle with options as desired: With[{a = Interval[1.01 + .18 {-1, 1}], b = Interval[.92 + .11 {-1, 1}], c = Interval[2.2 + .2 {-1, 1}]}, Plot[{Min[a + b*x + c*x^2], 1.01 + .92 x + 2.2 x^2, Max[a + b*x + c*x^2]}, {x, -5, 5}, Filling -> {1 -> {3}}, FillingStyle -> Darker, PlotStyle -> {None, Red, ...


6

Here's one way to do it: a = {1, 2, 3, 4, 5}; b = {2, 0, 40, 5, 0}; x = Quiet[Table[a[[i]]/b[[i]], {i, 1, 5}]]; Position[x, ComplexInfinity] {{2}, {5}} Basically, ans contains the values plus the errors, which in this case are ComplexInfinity. You can locate the errors using Position, in this case, at positions 2 and 5. If you want to check for any old ...


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]]]; ...


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