Check[expr,failexpr,checkedMessages] will evaluate expr and if it encounters any of the messages in checkedMessages it will evaluate failexpr and return that instead.
But how can I Check for all messages except let's say one?
Quiet has the syntax Quiet[expr,All,notQuiet] that will suppress all messages except those in notQuiet, but Check does not have this option.
A workaround is to use Quiet inside Check:
Check[Quiet[expr,ignoredMessages],failexpr,checkedMessages]
which will trigger for any message that is contained in checkedMessages but not in ignoredMessages.
However this has the unintended side effect of also (obviously) Quieting the ignoredMessages.
So for instance the desired function
myCheck[expr_,failexpr_,checkedMessages_,ignoredMessages_]:=..?
should do this:
myCheck[1/0,1,All,{}]
(* show message Power::infy *)
(* return 1 *)
but
myCheck[1/0,1,All,{Power::infy}]
(* show message Power::infy *)
(* return ComplexInfinity *)
(This is a more succinct question about one of the issues in this question)
Check[]does not abort an evaluation but let's it run its course; then itfailexpris evaluated.Catch@Check[Check[res = expr, Throw@res, ignoredMessages], failexpr, messages]might work for you. (It's probably better to useThrowandCatchwith tags.) – Michael E2 Nov 06 '18 at 11:34Checkdoesn't abort the evaluation, that was sloppy. I don't need it to though, I've added two examples. – Jansen Nov 06 '18 at 13:06Checkaborts the evaluation, it doesn't and I don't need it to. I want the functionality ofCheckbut just with the addition that it can check for all messages except some specified list. Writing your suggestion asmyCheck[expr_, failexpr_, checkedMessages_,ignoredMessages_]:= Block[{res}, Catch@Check[Check[res =expr, Throw@res, ignoredMessages], failexpr,checkedMessages]]gives an error in my examples regardingAll. This can be solved by replacingcheckedMessagesbyEvaluate[checkedMessages/.All->Sequence[]]... – Jansen Nov 06 '18 at 13:21ignoredMessages, but then it returnsComplexInfinityin both examples above. – Jansen Nov 06 '18 at 13:21checkedMessagesare emitted is to returnfailexpr, yes? – Michael E2 Nov 06 '18 at 13:57failexprwithout suppressing any of the messages, even the ignored ones. – Jansen Nov 06 '18 at 14:03