Tag Info

Hot answers tagged

12

I have answered almost exactly this question (somewhat more general one, if we interpret this one as being concerned only with Throw and Catch) here. Since you asked a more narrow one, I feel it may be appropriate to borrow a part of my answer, to have it here. The method This method is IMO almost never appropriate for the top-level functions that are ...


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

A simple method for accomplishing this is to have Message Throw an error when it is called, interrupting the current execution. Here is a replacement for Check which does that, with the same calling signature: ClearAll[InterruptingCheck] SetAttributes[InterruptingCheck, HoldAll] InterruptingCheck[expr_, failexpr_, msgs : {___MessageName } : {}] := ...


5

I Trace-ed the Information[ f ] command both before and after an invocation of f (with a simpler definition f[x_] := Legended["123", x]). By comparing the results, I found a function System`Dump`makeusageboxes: which reduces to another function BoxForm`MakeBoxesWithTextFormatting: Attributes[BoxForm`MakeBoxesWithTextFormatting] = {HoldAllComplete} ...


5

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


4

I don't know Python, and there's one aspect of the Python code I don't get; but this does what the last paragraph describes. safeeval[fn_, x_, epsilon_] := ReleaseHold @ Catch @ Quiet @ Check[ fn[x], Check[ ReleaseHold @ Catch @ Quiet @ Check[ fn[x + epsilon], Throw @ Hold[fn[x - epsilon]]], Throw @ ...


4

It depends on how you are doing the iteration. For example, if you were using Nest, then it could be replaced by NestWhile. If you are willing to modify the function, the you can explicitly test for divergence in the function, for instance f[x_] := Clip[x^2, {-1000, 1000}] will bound f. If it is a user defined function, then you can wrap it in the Clip ...


3

It looks like you already have your solution, and a rather elegant one at that, but perhaps what is yet lacking is a convenient method that does not require manually writing that nested structure. We can use recursion. The basic form looks like this: SetAttributes[errorTry, HoldAll] errorTry[a_, b__] := Quiet @ Check[a, errorTry[b]] errorTry[x_] := x ...


1

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



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