bazza2jw / open62541Cpp

C++ Wrappers for Open62541 Version 0.3
35 stars 21 forks source link

Use exceptions instead of lastError and lastOk #16

Closed Pro closed 2 years ago

Pro commented 2 years ago

This MR changes the error handling from using return values to Exceptions. I.e., if an open62541 method returns anything else than UA_STATUSCODE_GOOD, the code does throw an exception which can be catched in the user code.

Currently the code is only throwing a StatusCode Exception, but this can easily be extended to map specific status codes to specific exception types.

Error via exception the big advantage that the user code can be much more simplified, as it does not require complex multi-level if checks (see updated examples in this MR).

Also, the previous code sometimes did not update the lastError value, and therefore it was still set from an unrelated previous method which leads to difficult-to-find errors.

ping @Mo-Tay as he also contributed some changes.