-
Notifications
You must be signed in to change notification settings - Fork 183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
highspy: unpythonic error handling #1906
Comments
For
My view is that, since After being passed a file name that does not exist, the incumbent model in HiGHS remains empty. Hence no error occurs when Note
If Python users debug by observing exceptions rather than checking return codes, then there's a discussion to be had. If HiGHS raised an exception, I think that C++/C programmers would be unhappy. Of course, I've admitted that HiGHS generally doesn't trap memory allocation errors, but these are very unlikely since problems that don't fit into memory are generally far too big to solve. We have seen this once, and added memory allocation checking to presolve. We should add it to the model "addition" methods. |
Exactly. I'd say
which raises
I don't argue for the C++ code of highs to throw exceptions. It can be done solely in the python wrapper as already happens for some functions like |
Thanks. Useful dialogue. Team HiGHS can do little in Python, so it's really up to the community to propose a better way of handling exceptions |
The following python program exits with status code 0.
and this output:
Clearly, exit code 0 is wrong as this was not a successful run. What should have been done instead?
For a simple script, this would work:
But if this code is part of a larger program that shouldn't crash right away, using an assert is not an option. If, for example, one wants proper exceptions to be handled by the caller, one could start guessing what the problem might have been and raise an appropriate exception:
But then one has to add this boilerplate code around every highspy call and has to think about what the proper exception should be.
This problem is not limited to
readModel
, but (almost?) all highspy functions. For example, I couldn't even guess whyaddVar
could not return Ok. I guess that would be some exceptional circumstance, which means it would be ok to raise an exception.All in all, the current error handling in highspy feels like using a C API and not a python API. What's the solution? One way would be to define a HighsException and write python wrapper functions around every highs call, perform the error handling in the wrapper, and raise HighsException if necessary. But since there are people around here who are more knowledgable than me in writing python APIs, they might come up with better ideas.
EDIT:
addVariable
is already a wrapper that works like this and raises a generic Exception. There is a handful of other functions that work like this.The text was updated successfully, but these errors were encountered: