4 ms·
You mean like when a function silently returns -1 to indicate failure and then you wonder why your program returned a wrong result (if you're lucky enough to ev
by a-priori 18y ago
You mean like when a function silently returns -1 to indicate failure and then you wonder why your program returned a wrong result (if you're lucky enough to even notice)?
This is the fault of the programmer who wrote the function, not of the language.
- Hexstream 18y agoWhat? What can a programmer do besides returning a "special" value such as -1 in a language like C with strict types and that doesn't support exceptions?
- jwilliams 18y agoYou don't need an exception mechanism to deal with this - good libraries will let you find out the exact condition (if you do get a -1 for example). By the same token, dozens of Java libraries only throw ABCException, with no other information... Might as well be -1.
- wvenable 18y agoBut you can't ignore the ABCException, your app will crash. If you ignore the -1, then you're app will happily continue on doing something wrong.
- DarkShikari 18y agoSure you can ignore it. You don't want to know the number of times I've seen people using the "try, catch, toss away the exception" structure in Java code.
- mstevens 18y agoBut you have to actively decide to ignore it. Whereas when you're dealing with return codes, the default is to ignore.
- silentbicycle 18y agoIn OCaml, you're required to do something with the return from any non-void function. You can just say "ignore (f x)" to discard the result, so it's not hugely inconvenient, but causes suspect code to accumulate warning signs. It seems like a good compromise.
- mstevens 18y agoThis also seems like a pretty good strategy. I really need to take that OCaml book off the shelf.
- jwilliams 18y agoWell I don't know your experience, but most Java developers I've seen either throw the exception, or suppress it... If they throw it, it eventually ends up as ABCGenericException and you've probably got no chance to reasonably recover. If you suppress it you end up with a nasty side effect (a null somewhere perhaps) that causes the app to bomb in another unexpected piece of code. Exceptions are a language semantic - in itself it doesn't make your code robust. You still need to apply it correctly. But! The reality is the same applies to C too. What the original author was alluding to what that the mentality of C encourages coders to look deeper into their errors.
- silentbicycle 18y agoFalse. In languages with better type systems (e.g. ML and Haskell), you generally use the "Option" (OCaml) or "Maybe" (Haskell) type in situations like these. (Example code is OCaml) type 'a checked = Pass of 'a | Fail (* very much a toy example *) let even x = (x mod 2 == 0) let can_fail x = match x with | 0 -> Fail | x when (even x) -> Pass (x * 10) | x -> Pass (x) # can_fail 2;; - : int checked = Pass 20 # can_fail 3;; - : int checked = Pass 3 # can_fail 0;; - : int checked = Fail # The languages infers where you're passing around option types (such as "int checked" above) and forces the calling function to account for any potential failures. Rather than just Pass 'a and Fail, it could be Pass, File_Not_Found, Access_Denied, etc., and (Unknown_Error string). OCaml also has exceptions, but the combination of union types and required exhaustive matching covers most cases.