3 ms·
It's a pity that the section on exceptions didn't do a more detailed analysis on the criticism of exceptions; in my personal view I haven't liked exceptions, in
by tankenmate 11mo ago
It's a pity that the section on exceptions didn't do a more detailed analysis on the criticism of exceptions; in my personal view I haven't liked exceptions, in a similar vein to how I'm not a huge fan of the implementation of POSIX signals or setjmp / longjmp.
Although I very much see the reason why it was developed, in effect it comes closer to a glorified form of "come from" (and people thought that goto was considered harmful).
- somewhereoutth 11mo agoAlthough INTERCAL's "come from" provided the opportunity to implement a gloriously concise multi-threading syntax/mechanism [1]! [1] https://esolangs.org/wiki/Threaded_INTERCAL https://esolangs.org/wiki/Threaded_INTERCAL
- adastra22 11mo agoHow do you feel about algebraic effects?
- vlovich123 11mo agoExceptions done well should outperform return value checking. However it’s very difficult to make it perform well and for some reason people prefer -> Result<T, E> instead of -> T throws E which is basically the same thing.
- hibikir 11mo agoThe difference is the monadic capabilities of the result type. Thrown exceptions pepper codebases in ways that are even more unfortunate than monadic composition, which is already kind of iffy, but at least has generic transforms for error recoveries, turning errors to successes of a different type and so on. You end up with far less boilerplate.
- vlovich123 11mo agoI’m not so sure. The page goes over how it works in Scala 3 and it’s a little bit cleaner. But there is some nicety in handling return and exception uniformly in some cases.
- gf000 11mo agoWell, PLs with effect systems are basically meant to solve exactly this problem. E.g. a generic `map` function can become throwing if it handles throwing lambdas, but are otherwise non-throwing - this is pretty much the main pain point with Java's checked exceptions.
- codr7 11mo agoBasically the same thing except the former is pessimistic and the latter optimistic.
- naasking 11mo agoThey're not the same since exceptions typically require non-local control-flow, where Result types require local control flow.
- mabster 11mo agoI'm the opposite - I really like checked exceptions in Java because it's very easy to see how developers are handling errors and they also form part of the function signature. Most functions will just pass on exceptions verbatim so it's better than error return values because with them the entire codebase has to be littered with error handling, compared to fewer try catch blocks. setjmp, etc. are like unchecked exceptions, so I'm also not a fan, but I use this occasionally in C anyway.
- phanimahesh 11mo agoWhy would errors as return values have to propagate any farther in the codebase compared to errors as exceptions? If exceptions can be handled, so can the value based errors.
- tialaramex 11mo agoThe criticism as I understand it isn't about where the errors are actually handled but the ceremony needed in an errors-as-values model, most obviously in Go where you've got to write a couple of lines of test and early return explicitly for each such possible error, compared to C++ where you write nothing. Rust's Try operator is the minimal ceremony, a single question mark acknowledges that we might not succeed and if so we return early - but there was still ceremony for each such early return. I happen to think exceptions are inherently a bad idea, but for a different reason.
- poncho_romero 11mo agoWhat reason is that?
- mabster 11mo agoYou nailed it - that's my criticism :) I've worked with a lot of code like this (particularly C libraries and litanies of return codes), and it's fine... But I prefer something like Java-style exceptions. And with Java lambdas or Kotlin the trend is unfortunately away from checked exceptions these days... I too am interested in your other reasons!
- JoelJacobson 11mo agoI think the sweet spot is to use exceptions for bugs. If the error is expected, make it data.
- tialaramex 11mo ago"Exceptions should be exceptional" gets to the heart of the problem with the entire concept of Exceptions for non-trivial pieces of software where there's more than a single programmer maintaining the complete system. Now the library programmer has to guess whether when you - the application programmer try to Wibble a Foozle - will have ensured all the Foozles can be Wibbled, and so not being able to Wibble the Foozle is an exceptional condition, or whether you want to just try to Wibble every Foozle and get an error return if it can't be Wibbled... One option is for every such condition you bifurcate the API. Instead of three types with a total of eight methods, maybe there are six conditions for two of those methods, and so now it is three types with over 100 methods... ouch. Good bye documentation and product quality.
- ocharles 11mo agoSo - exceptions are for invariant violations? I'm essentially trying to work out what it is that makes something "exceptional"
- gf000 11mo agoWell, this obviously depends on a given programming language/culture, but in my mind I would say in case of parsing a string to an int it is an expected case that it could fail, so I would model it as a Return type of an Int or a ParsingError, or something like that. Meanwhile, for a function doing numerous file copies and network calls I would throw an exception, as the number of possible failure cases are almost limitless. (Like you surely not want to have an ADT that models FileSystemFullErrors and whatnot). It so happens that in this particular example one is pure and the other is side-effecting, but I'm not convinced that's a hard rule here.
- 11mo ago
- adalacelove 11mo agoEverybody chooses a favorite depending on their domain. A function executes, and some error happens: - Return error value: try to handle the error ASAP. The closer to the error the more detailed the information. Higher probability of recovery. Explicit error code handling throughout the code. Example: maybe you try again in one millisecond because the error is a very low probability but possible event. - Exception: managing errors requires a high-level overview of the program state. Example: no space left on device, inform the user. You gather the detailed information where the error happened. The information is passed as-is or augmented with more information as it bubbles up the stack until someone decides to take action. Pros: separate error handling and happy path code, cleaner code. Cons: separate error handling and happy path code, unhandled errors. Worst case scenario: you program in C. You don't have exceptions. You are forbidden to use setjmp because rules. A lot of errors are exposed directly to the programmer because this is a low-level language. You return error codes. Rules force you to handle every possible return code. Your code gets incorporated as an appendix to the Necronomicon.
- joostdevries 11mo agoThis 2003 interview with Anders Hejlsberg gives considerations why he didn't add checked exceptions to C#. https://www.artima.com/articles/the-trouble-with-checked-exceptions https://www.artima.com/articles/the-trouble-with-checked-exc...