4 ms·
It helps a lot with idiomatic C error handling. You'll often have a function that does something, returns 0 on success, and an error code on error. It's nice
by amalcon 17y ago
It helps a lot with idiomatic C error handling. You'll often have a function that does something, returns 0 on success, and an error code on error. It's nice to be able to do the assignment in the if statement there.
There's no good reason for it in any language built with exceptions from the ground up, though.
- silentbicycle 17y agoOr with languages that have a better mechanism for returning multiple values. (Passing in pointers to optional results? Ick.) There's in idiom in Lua to have functions return either <true, result> or <false, error message> (as two arguments, not a tuple) - consequently, it's common to say e.g. "status, f = assert(open_resource(arg, arg2))"; if it fails, the assert will fail, and open_resource will provide the error message. In statically typed languages, returning a union type (e.g. Error | Pass of Result) also works well. Sometimes exceptions are simpler, sometimes not.