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


5

How about using "RuntimeErrorHandler": f = Compile[{{x, _Real}, {y, _Real}}, Log[(x - y^2 - 2. x)^2]/(y x^2 - 2 (x + y) - y^2 + 3.), "RuntimeOptions" -> {"RuntimeErrorHandler" -> Function[Throw[$Failed]]} ]; Catch[Quiet@f[-196, 15]] // AbsoluteTiming (* {0.000019, 0.0000116843} *) Catch[Quiet@f[-196, 14]] // AbsoluteTiming (* {0.000051, ...


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


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

I use \$MessagePrePrint = StandardForm since without that the real number 1.5 is displayed in a message as 1.5`. However, you might have $MessagePrePrint set to something else. Check is used to control what should happen when a built-in message occurs. Quiet prevents the built-in message from being displayed. I made a pure function (i.e. #1,#2,& ...


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

Okay, so the solution was that instead of using NestList one simply has to use NestWhileList, because the latter has a built-in possibility of running a test at each iteration, and it does stop when it fails! In my case, I wanted to exit the nesting as soon as there was an overflow, or if the highest number of iterations was reached (here a thousand). So all ...


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