c++ - The error representation and handling -
i designing cross-platform application protection library communicate dongle. dongle able send , receive blocks of data, create , delete directories , files inside dongle's internal file system, etc. put simple, main dongle object has methods implement of these functionalities. unfortunately, of these methods may fail. right thinking if :)
the obvious solution return boolean type result of these methods indicate if fail or success. however, leads loss of details nature of error.
the next possible solution return error code, there more 30 types of errors. also, need design separate function convert of these return codes more readable string representation.
the third solution return boolean type result, keep error state object in main dongle object possible retrieve using getlasterror() method. considering using one.
now question. there other reasonable error representation , handling patterns , suggest me use in case?
thanks, ilya
of options have presented, best use return codes. typically return code of 0 indicated success, , each other return code has own positive integral value. 30 error conditions isn't many. you've said, you'll have write code translate these codes human-readable, have anyway.
i consider writing exception hierarchy instead of error codes, however. exceptions can more expressive return codes, , code can cleaner if done properly. typically design library there different exception class each type of error return condition, each derived std::exception. what() method gives place put human-message building, , type of exception describes error.
there tell exceptions "only" exceptional circumstances, computer caught fire, or something. hotly debated assertion. i'll tell nonsense. because named "exception" doesn't mean computer has catch fire in order use it. exceptions give lot of benefits, 1 of biggest being stack unwinding. preventing using them because of arbitrary line errors "bad enough" silly.
Comments
Post a Comment