# Detecting error returns in library routines?

**URL:** <https://community.wolfram.com/t/detecting-error-returns-in-library-routines/24316>\
**Category:** Questions\
**Tags:** optimization, tuning-and-debugging\
**Created:** [July 11, 2024, 11:53pm UTC](https://community.wolfram.com/t/detecting-error-returns-in-library-routines/24316 "2024-07-11T23:53:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cscotthuddleston](https://avatars.discourse-cdn.com/v4/letter/c/958977/32.png) [@cscotthuddleston](https://community.wolfram.com/u/cscotthuddleston)\
**Post date:** [July 11, 2024, 11:53pm UTC](https://community.wolfram.com/t/detecting-error-returns-in-library-routines/24316/1 "2024-07-11T23:53:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![eric3](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/eric3/32/12760_2.png) [@eric3](https://community.wolfram.com/u/eric3)\
**Post date:** [July 12, 2024, 2:59am UTC](https://community.wolfram.com/t/detecting-error-returns-in-library-routines/24316/2 "2024-07-12T02:59:12Z")

</div>

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` ([Check—Wolfram Documentation](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?

---

<div class="post-metadata">

**Author:** ![cscotthuddleston](https://avatars.discourse-cdn.com/v4/letter/c/958977/32.png) [@cscotthuddleston](https://community.wolfram.com/u/cscotthuddleston)\
**Post date:** [July 12, 2024, 4:42am UTC](https://community.wolfram.com/t/detecting-error-returns-in-library-routines/24316/3 "2024-07-12T04:42:45Z")

</div>

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.
