Replies: 3 comments 7 replies
-
An interesting thought. I've grown up with the idea that throwing exceptions was always to be avoided. However, I've noted you view on the convenience within Python. However, as we've now seen in relation to #1367 , there are sure to be many users who will object to such "fatal" terminations of HiGHS. |
Beta Was this translation helpful? Give feedback.
-
In general, -1 from me for exceptions. Requiring exception handling to be enabled removes HiGHS from consideration in most safety critical code bases (if you care about that) and common C++ coding standards. If you're not wanting to appeal to the safety critical crowd and binary size isn't a huge issue, then exceptions may be desirable for the reasons mentioned above. |
Beta Was this translation helpful? Give feedback.
-
My 2 cents on this right now is that as long as the C API needs to be supported, there isn't much of a benefit using exceptions since we'd still need an error structure for the C API, so it might as well be used on the C++ side as well (to be fair I think exceptions are ever so slightly faster in pure C++ though). |
Beta Was this translation helpful? Give feedback.
-
I have been looking into the
highs
code and I was wondering if there have been any previous discussions on throwing exceptions in the code. As a concrete example consider the following code (inscipy
) which crashes the interpreter (described in scipy/scipy#15888):Which is rather elegantly fixed by throwing and re-throwing an appropriate exception (demonstrated in this PR to the SciPy HiGHs fork). Some things to keep in mind are that they are thread safe, and have generally zero overhead (unless an exception is actually thrown). They also simplify error handling within the code and for python bindings (which no longer need to check specialized error structures).
One caveat is that the
C
bindings cannot handle exceptions, and so they need to be caught and translated to an error status code. However, on whole exception handling makes the code more expressive and easier to debug / work with. It should also be noted that it does not need to (immediately or ever) replace the existingResult
statuses and return values, which indicate more logical constraints and user-error. There's also extensive support for exceptions in Pybind11 sohighspy
will benefit as well.If adding exceptions is acceptable, I'll make a PR similar to the SciPy fork here.
Some references for the implementation:
[1] https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#S-errors
[2] https://learn.microsoft.com/en-us/cpp/cpp/errors-and-exception-handling-modern-cpp?view=msvc-170
[3] https://learn.microsoft.com/en-us/cpp/cpp/how-to-interface-between-exceptional-and-non-exceptional-code?view=msvc-170
[4] https://isocpp.org/wiki/faq/exceptions
Beta Was this translation helpful? Give feedback.
All reactions