Message Boards Message Boards

1
|
166 Views
|
2 Replies
|
4 Total Likes
View groups...
Share
Share this post:

Detecting error returns in library routines?

How does one write code to detect abnormal or error return conditions from library routines? My immediate need is the case when LinearOptimization finds no solution: LinearOptimization::nsolc, but I'd like to know how to handle error/abnormal returns more generally.
It's not clear to me, from the online documentation, how to do this. Something in the Confirm family, perhaps?
Thanks.

POSTED BY: Scott Huddleston
2 Replies
Posted 15 days ago

It kind of depends on what you mean by "abnormal" and "error". In the case of LinearOptimization::nsolc, that's a message id. If you want to deal with handle situations where messages were generated, the typical strategy would be to use Check (http://reference.wolfram.com/language/ref/Check.html). For other "failure" cases, Confirm with an enclosing Enclose would be typical.

So, for example,

Check[LinearOptimization[x, {x <= 0, x >= 1}, x], "whatever you want in the failure case goes here"]

You could use ConfirmQuiet to generate an actual Failure expression.

Enclose[ConfirmQuiet[LinearOptimization[x, {x <= 0, x >= 1}, x]], functionToApplyToFailure]

I understand that the documentation wasn't clear to you, but without knowing what you tried or what about the documentation you aren't understanding, I'm not sure what else to explicitly demonstrate. There are many examples in the documentation--have you tried them, or can you say which ones are confusing for you?

POSTED BY: Eric Rimbey

Thanks for the spot on examples and suggestions. A simple check (and turning off the message for cleaner output) handles my current need nicely. I hadn't known much about messages, hadn't known to look for them, and hadn't found a top level documentation node that guided me there. Your examples and the Messages documentation node are quite helpful.

POSTED BY: Scott Huddleston
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract