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

前端 未结 7 1975
庸人自扰
庸人自扰 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:57

    You should also consider a object/structure based error code variable. Like the stdio C library is doing it for FILE streams.

    On some of my io objects for example, i just skip all further operations when the error state is set so that the user is fine just when checking the error once after a sequence of operations.

    This pattern allows you to finetune the error handling scheme much better.

    One of the bad designs of C/C++ comes to full light here when comparing it for example with googles GO language. The return of just one value from a function. GO does not use exceptions instead it always returns two values, the result and the error code.

    There is a minor group of people who think that exceptions are most of the time bad and misused because errors are not exceptions but something you have to expect. And it hasn't proved that software becames more reliable and easier. Especially in C++ where the only way to program nowadays is RIIA techniques.

提交回复
热议问题