How to tell Mathematica to exit due to debugging error

Is there a parameter (I would expect this to be an environment variable) in Mathematica that will cause the kernel to exit due to a debugging error? Often I get debugging errors, and the laptop will continue to evaluate bad data, forcing me to restart the kernel.

+8
debugging wolfram-mathematica
source share
4 answers

You wrote:

First of all, I want my laptop to stop working on an error statement.

You can do this by specifying $AssertFunction as follows:

 $AssertFunction := Quit[] & ; 

Now the kernel exits when the statement fails:

 << ExampleData`FunctionWithAssert` Compute[ 1.0] (*=> kernel quits*) 

Alternatively, you can use Interrupt as follows:

 $AssertFunction := Interrupt[] & ; 

This allows you to abort or enter a cut (same as Dialog[] in Arnoud's answer).

You can also extend this approach to print a failed statement:

 $AssertFunction := (Print[HoldForm @@ #]; Interrupt[]) &; 
+4
source share

Following an idea taken from Mathematica :

 myMessage::debug = "Something went horribly wrong"; Unprotect[Message]; Message[mm : myMessage::debug] := Block[{$inMsg = True, result}, Message[mm]; Quit[]] /; ! TrueQ[$inMsg] Protect[Message]; 

Here, replace myMessage::debug with the actual type of message you want to intercept.

Normal message:

 Message[Power::infy] (* ===> StringForm::sfr: Item 1 requested in "Infinite expression `1` encountered. >>" out of range; 0 items available. >> Power::infy: Infinite expression `1` encountered. >> *) 

Your debug message:

 Message[myMessage::debug] (* ===> myMessage::debug: Something went horribly wrong [kernel quits...] *) 

Refresh

Assembler messages contain additional arguments, so you must catch them. And by the way, you should put the actual name of the message in the definition (without using the sample message that I used above):

 Unprotect[Message]; Message[mm : Assert::asrtfl, m___] := Block[{$inMsg = True, result}, Message[mm, m]; Quit[]] /; ! TrueQ[$inMsg] Protect[Message]; 
+13
source share

This answer assumes that with "debugging error" you mean that Message has been released. For example, a message is issued as a warning if you try to divide by zero:

 In[1]:= Print[1/0] Power::infy: Infinite expression 1/0 encountered. >> ComplexInfinity 

Note that ComplexInfinity was printed by the Print statement, even though a warning message was issued indicating that the calculation continues after an β€œerror”.

I won’t reuse @Sjoerd's excellent answer, which shows how to set up a Mathematica session so that the kernel exits if any message fails with an evaluation. If you want to be more selective and get out of the kernel only if a particular rating triggers a message, then the following function may be useful:

 ClearAll[checkQuit] SetAttributes[checkQuit, HoldFirst] checkQuit[expr_, HoldPattern[messages_:Sequence[]]] := Check[expr, Message[checkQuit::quit]; Quit[], messages] checkQuit::quit = "The kernel is being shut down!"; 

Using this definition, you can make the kernel fail after a message is issued with a specific rating:

 In[37]:= checkQuit[Print[1/0]] Power::infy: Infinite expression 1/0 encountered. >> checkQuit::quit: The kernel is being shut down! 

If it can only be convenient to exit the kernel if certain messages are released. To this end, checkQuit accepts an optional second argument, which can be used to indicate messages of interest:

 In[6]:= checkQuit[{}[[10]], Power::infy] Part::partw: Part 10 of {} does not exist. >> Out[6]= {}[[10]] 

Note that the kernel was not output, as the message did not match Power::infy . But note:

 In[7]:= checkQuit[1/0, Power::infy] Power::infy: Infinite expression 1/0 encountered. >> checkQuit::quit: The kernel is being shut down! 

Here, the kernel terminates because the specified message appears. You can filter multiple messages or even predefined message groups. See Check for details.

+8
source share

In addition to the other answers you can explore, use:

 $MessagePrePrint 

Allows to execute code when any message is triggered.

For example, you can evaluate this:

 $MessagePrePrint := (Print[Stack[_]]; Dialog[]) 

What prints the evaluation stack (sometimes very large!) Of the code that is being evaluated. Then the dialogue function puts you in a sub-evaluation cycle, allowing you to check the values ​​of the variables. To exit the dialog loop, you can evaluate:

 Return[] 

which returns you to the main loop (the main assessment then ends or is interrupted by a new message).

+7
source share

All Articles