3 ms·
>For example COM has a 32-bit integer called HRESULT that all methods return. The high bit says something is an error. A few bits in the high 16 bits indicate w
by lmm 13y ago
>For example COM has a 32-bit integer called HRESULT that all methods return. The high bit says something is an error. A few bits in the high 16 bits indicate where an error is from. The low 16-bits are private to whatever space is specified in the aforementioned bits. So if you take an error like 0x80070002 you can say it's an error, from the Win32 subsystem, and the error code is 0002 or file not found in this specific instance. Another component can set the high bits differently and end up with their own 16-bit error codes. I find when I write COM code the errors are much more meaningful than in a high-level language because it's all very explicit, you're constantly thinking about it at every function call, whereas in exception-based systems it's all just very haphazard and hand-wavy.
If you want to have an exception that just contains an error code you can do that (e.g. java's SQLException works that way). Personally I find it much more useful to be able to give errors textual name, and declare a hierarchy relationship between errors.
> Actually this "early return" pattern is quite bad in plain C, because if you have N allocations for example, each "return" statement needs to free between 0 and N buffers before leaving the function. That gets very maddening. One way to avoid this is to have only one "return" statement at the end of your function and free any loose buffers if-and-only-if they are non-null. This involves either constantly re-checking some error status, or using a goto to jump to the end of your function when something fails. IMO the 2nd is much cleaner. (Let that wrap around your high-level-language brain... but also know that any large C only code base is doing the same thing if they are sane; grep the Linux kernel for "goto" and you might start to get a picture... and while you're bashing C error handling and probably thinking ill for me for pulling out "goto", please keep in mind your "exception" concept is really just a goto that crosses stack frames...) In the end this pretty well simulates what you'd do in destructors in a C++ code base following the RAII pattern.
Sure - exceptions exist to replace something you would otherwise have to use goto to do, that's pretty much the entire point. But they're better than goto for the exact same reason that a while loop is better than implementing a loop with goto.
> In my experience it, on the contrary the exception concept just makes sure you have no idea that stuff can fail, and every small failure in a "shockingly" unexpected place blows up your entire program and terminates the process. Or you have Java's goofy "checked exceptions" concept and so lazy programmers will introduce do-nothing catch blocks to shut up the compiler.
You have the exact same problem in C. Either programmers have no idea that library functions can fail and never check return codes, or you have something like gcc's warn_unused_result and lazy programmers introduce do-nothing error-code checks.
- asveikau 13y ago> You have the exact same problem in C. Either programmers have no idea that library functions can fail and never check return codes, or you have something like gcc's warn_unused_result and lazy programmers introduce do-nothing error-code checks You forgot the other option. The careful programmer knows they have to check return codes and is suspicious when it's not happening. With exceptions you have no idea when they can happen because it's not part of the function signature. Or in the case of Java, handling errors is like homework, a constant struggle to keep the nagging compiler happy, instead of a consistent style that you apply throughout a code base.