I have a program which can lead in certain cases to a division by zero. I would like to know if there is a way in Mathematica to make a conditional statement like the following one:

If[Warning == Power:: infy: Infinite expression 1/0 encountered, something, somethingelse]

As it is a complex piece of code borrowed from an old Paper from the Mathematica Journal, I would like not to search exactly what line is the cause of the warning and program what to do in this case.


Consider the following contrived example:

fun := If[EvenQ@RandomInteger[{1, 20}], 1/0, "Success"]

Quiet@Check[fun, "FAILED", {Power::infy}]

This function will generate a division by zero roughly half the time, randomly. You can evaluate it a few times, and you will see that when the division by zero happens, Quiet prevents the message from being displayed, but you still get the "FAILED" output to indicate the problem, which you can replace with whatever makes sense for you in your case.

| improve this answer | |
  • $\begingroup$ Thanks a lot so my question was not so stupid ! $\endgroup$ – cyrille.piatecki Jul 29 '17 at 19:32

Not the answer you're looking for? Browse other questions tagged or ask your own question.