TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Why Every Programming Language Sucks at Error Handling

14 pointsby kugurerdem2 months ago

6 comments

biwills2 months ago
The more I write software, the more I think errors should be first-class citizens (camp #2 from the OP&#x27;s post).<p>I&#x27;ve been using <a href="https:&#x2F;&#x2F;github.com&#x2F;biw&#x2F;enwrap" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;biw&#x2F;enwrap</a> (disclaimer: I wrote it) in TypeScript and have found that the overhead it adds is well worth the safety it adds when handling and returning errors to users.<p>That said, I see parallels between the debate about typed vs. non-typed errors and the debate of static typing vs. dynamic typing in programming languages.
评论 #43336888 未加载
smackeyacky2 months ago
There is another class of error that has been poorly implemented since the demise of Smalltalk - exceptions during testing that can be fixed by the programmer and allow the program execution to continue.<p>In a lot of the non-trivial systems I&#x27;ve worked on, the setup to do debugging&#x2F;development work to get to a particular part of the code can often be arduous and time consuming, meaning that if you made a mistake and have to restart your debugging session you then have to go through the setup process again which radically slows down progress on thorny issues.<p>In Smalltalk it was possible to take an unhandled exception, re-inject a more suitable value and continue debugging so that each &quot;session&quot; was far more productive and the setup issue became much less onerous.<p>Sadly, .NET, Java, Javascript and Python can&#x27;t even come close to the power of that ability to catch an exception, modify the object and continue execution. It&#x27;s a great loss in my opinion. So much of the Smalltalk programming environment has been copied into modern languages but not re-entrant exceptions.<p>Now, while it was very powerful it could also be heavily abused including at run time, but I still miss that ability.
评论 #43338311 未加载
评论 #43341550 未加载
评论 #43337744 未加载
ahefner2 months ago
There&#x27;s still a few Common Lisp features the mainstream would benefit from ripping off. Relevant here is the condition system. Conditions and restarts are still foreign enough that it&#x27;s hard to explain to people why the &quot;debugger&quot; was a fundamental part of the lisp machine UI, and how much sense that made, and harder still to fathom how you&#x27;d adapt that to Unix, where it would morph into some kind of weird IPC tied into the command shell.<p>It&#x27;s not just about being able to stop your program and hot-patch code to recover from errors - this should be trivial in any dynamic language. Rather, it&#x27;s about being able to composably control how to recover from exception conditions (or, really, any branching behavior) both interactively and programmatically in a unified framework.
dfe2 months ago
I don&#x27;t agree with the criticism of Java&#x27;s exception handling, since Java literally does make exactly the distinction between &quot;1. A Bug in the system&quot; as RuntimeException, and &quot;2. A faulty situation that can&#x27;t be avoided&quot; as all other Exception, particularly IOException.<p>Although the same statement is used to catch both, you only catch both if you catch Exception. If you catch IOException or whatever other exceptions you need to catch and you opt to handle them, then RuntimeException will still propagate.<p>It is only a matter of understanding that this is the distinction, and writing the bodies of your catch blocks accordingly.<p>And if you are only writing a prototype, declare that it throws Exception, or if you can&#x27;t, a catch (IOException ex) { throw new UncheckedIOException(ex); } really isn&#x27;t that bad.
评论 #43340793 未加载
sherdil20222 months ago
Rust does a great job here. I know it is based upon other languages but error handling in Rust doesn’t suck
评论 #43340817 未加载
xerokimo2 months ago
An error to me can be as simple as any time I need to write a range check so I don&#x27;t index out of bounds, that very act of range checking is an error check. In other words, any time you require a runtime check to prevent an operation that would otherwise be erroneous is an error<p>Philosophically, I don&#x27;t differentiate how you represent errors, they are all errors in the end. You can use unchecked exceptions, results, checked exceptions, error codes, whatever. I will abide by rules set in the code base&#x27;s guidelines, and I do have my preferences (unchecked exceptions), and I&#x27;ll also accept using one or the other for practical reasons (performance, or binary overhead), but I will never argue that it is absolutely best that one error handling scheme is reserved for uses ABC, and the other best for EFG.<p>A bug is one of two things, writing code that is erroneous despite having knowledge at compile time that it can&#x27;t happen such as, indexing into an array of known bounds with inputs that are known to never be out of bounds. Typically where asserts are used. You can promote these types of bugs to errors if it doesn&#x27;t harm your system stability, and they often are in order to avoid crashing. The other definition of a bug is just your program entering in an unforseen state.<p>I dislike using the words expected, unexpected, and such for error handling unless you&#x27;re talking about c++&#x27;s std::expected, because no one can agree what they mean, hence to me are useless definitions when talking about error handling. While the same can be said about definitions of errors and bugs, the practical uses tend to converge to an agreement on whether something is an error, or a bug.<p>From years of observing error handling:<p>- Most times I see people talk about it, from what I interpret from what people say, seems to have an emphasis on what operations can error out, rather than emphasizing what the state of your program should be when a unit of code errors out, which I believe should be more emphasized.<p>I typically define a unit of code as a function because that&#x27;s how most things already work and is easier to reason about. In other words, stop focusing on individual failures of the callers, rather treat the entirety of the callee as a failure. This reduces your function to usually only have 2 states, what should your program state be when everything succeeds, and what should it be when it fails.<p>That&#x27;s how you can interpret a lot of value based error handling functions already no matter how many operations can fail. With exceptions it means that, either your whole function must be in a try&#x2F;catch block, or it doesn&#x27;t have one at all.<p>- Value based error handling seems to re-invent some forms of unchecked exception features, with very good reasons, leading me to believe that they are on the same sides of the coin where given enough language and tooling support, they converge to the exact same thing.<p>- I do think it&#x27;s better to encode preconditions in some way visible to callers that&#x27;s not just through documentation. If you&#x27;re using types, which is my preferred way, known as dependent types. I&#x27;m not too familiar with them, but apparently requires a proof solver for it to work entirely at compile time. I wouldn&#x27;t mind them turning into runtime checks, and if you have overloading enabled, allows runtime checks to mostly occur once, at construction time.