I have a complicated function which contains many calls to NIntegrate
, NSolve
, FindRoot
and other superfunctions in many places. My function is organized in such a way that every call to a superfunction is assigned to unique Block
ed variable. Sometimes these functions produce Messages
but it is not obvious which call has generated the message. I wish to find a straighforward way to know which call has generated the message. It would be great to include this information in the Message
itself but just printing this information before/after the Message
would be sufficient.
I know that there is undocumented function Internal`HandlerBlock
which seemingly can do such tasks but I do not know how to restrict it to handle only printed messages.
For example,
Internal`HandlerBlock[{"Message",
If[MatchQ[#, Hold[_, True]], Print[#]] &},
NIntegrate[Cos[20000 x]/Sqrt[x], {x, 0, 1}]]
Produces many prints but only one of them corresponds to the actually printed one Message
.