Why is it better to throw an exception rather than return an error code?

前端 未结 4 827
小蘑菇
小蘑菇 2021-01-31 08:44

Legacy error handling tends to follow the method that all functions return a code depending on success/failure. You would check this code and handle (if an error) appropriately

4条回答
  •  一向
    一向 (楼主)
    2021-01-31 09:25

    Here are a couple of reasons

    • Ignoring an exception requires action by the developer while ignoring a bad returning value requires exactly 0 action. This, in theory, makes it more likely a developer will handle an error vs. ignoring it or not even realizing it was happening.
    • Provides a cleaner separation between the point of an error and the handling. It doesn't force manual propagation of the error at every point in between.
    • Exceptions can a larger and richer information payload than a simple error code. There are ways to do this with error codes but it's more of an afterthought and is a bit cumbersome.

提交回复
热议问题