Is GetLastError() kind of design pattern? Is it good mechanism?

前端 未结 7 2139
庸人自扰
庸人自扰 2021-02-12 15:27

Windows APIs uses GetLastError() mechanism to retrieve information about an error or failure. I am considering the same mechanism to handle errors as I am writing A

7条回答
  •  轮回少年
    2021-02-12 15:56

    The GetLastError pattern is by far the most prone to error and the least preferred.

    Returning a status code enum is a better choice by far.

    Another option which you did not mention, but is quite popular, would be to throw exceptions for the failure cases. This requires very careful coding if you want to do it right (and not leak resources or leave objects in half-set-up states) but leads to very elegant-looking code, where all the core logic is in one place and the error handling is neatly separated out.

提交回复
热议问题