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.

The Throw Keyword Was a Mistake

27 pointsby _1over 5 years ago

20 comments

kova12over 5 years ago
I disagree. Throw keyword is the greatest invention, and makes programming so much better. Error codes are the evil. Exceptions allow to separate business logic from error handing in a most effective manner. Author likely have not ever worked on large codebases where the error handling was messed up beyond any repair due to having to pass error codes every friggin where
评论 #22167160 未加载
评论 #22167325 未加载
评论 #22167168 未加载
评论 #22167382 未加载
评论 #22167231 未加载
al2o3crover 5 years ago
<p><pre><code> only the language runtime and operating system would be allowed to raise exceptions </code></pre> So you still need all the exception machinery, but &quot;normal&quot; code can&#x27;t use it? Seems like a terrible situation.<p>Also, who&#x27;s in charge of deciding what&#x27;s &quot;language runtime&quot; and what isn&#x27;t?
评论 #22170516 未加载
评论 #22167248 未加载
评论 #22167120 未加载
mrkeenover 5 years ago
&gt; The Year Everything Crashed<p>Which year?<p>&gt; If you were using computers in the years that followed you remember the time well even if you didn’t know the reason; a lot of software that had been stable and solid before started crashing and misbehaving.<p>I think I missed this.
评论 #22167273 未加载
bluestreakover 5 years ago
Throw in Java is very inexpensive. Expensive part is creating new instance of exception, which figures out stack trace and creates bunch of immutable strings.<p>There is a technique of throwing existing static instance of exception, which generates no garbage and two orders of magnitude faster than traditional java approach:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;questdb&#x2F;questdb&#x2F;blob&#x2F;7560895ee400c82b0e5e39440eaf6c55bbbb7d7d&#x2F;core&#x2F;src&#x2F;main&#x2F;java&#x2F;io&#x2F;questdb&#x2F;cutlass&#x2F;text&#x2F;types&#x2F;InputFormatConfiguration.java#L207" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;questdb&#x2F;questdb&#x2F;blob&#x2F;7560895ee400c82b0e5e...</a><p>The only downside is that stack trace is effectively defunct.
dkulchenkoover 5 years ago
I love the way Elixir&#x2F;Erlang handles this, personally.<p>If the error is expected&#x2F;should be handled, just add the error to your pattern match and handle it right there (or bubble it up, if appropriate).<p>Truly exceptional situations where something &quot;should never happen&quot; won&#x27;t be pattern matched against, BEAM will crash the process, and your supervisor will get you back to a good state.<p>Feels so much cleaner than either exceptions everywhere or easy-to-ignore error values.
fxtentacleover 5 years ago
I strongly believe that the Go approach is the correct way to go. Every function that can fail should return an error code and then the programmer invoking said function is expected to handle the error code. It also used to be that way back in the old C &#x2F; C++ days before SEH made it messy like Java.<p>And the beauty of using statically typed languages with error code return values is that one can use automated tools to find all the places where error codes are being ignored. So then random error conditions sprinkled throughout business logic go back from &quot;this is how Java is done&quot; to &quot;this is a code smell&quot;.
评论 #22167378 未加载
评论 #22167322 未加载
评论 #22167241 未加载
评论 #22167341 未加载
评论 #22167514 未加载
评论 #22167237 未加载
DictumMortuumover 5 years ago
I worked at a telecom company. They had a program written in java for generating the initial customer configuration.<p>Among other stuff it used exceptions as control flow and it made me make extraordinarily high estimations for seemingly simple changes.<p>Anyway, I don&#x27;t think I agree with the article - it&#x27;s again the matter of the knife and how you use it.
评论 #22168903 未加载
评论 #22167730 未加载
scarejunbaover 5 years ago
We&#x27;ve moved past it now with having errors just be part of the type and signature so you can use language tools to deal with them like any other type instead of having special syntax. I like that. It allows you to express certain concepts more easily. For instance, &quot;in case of failure, use blank&quot; looks nicer in Rust than in Java, and so you&#x27;re more likely to use it. Java is also sort of moving this way because checked-exception methods don&#x27;t play well with Streams syntactically.
kazinatorover 5 years ago
&gt; <i>Structured Exception Handling (SEH) was born.</i><p>This, including the abbreviation, is the name of a feature of the Windows API.<p>The author is confused between exception handling in programming languages, and exception handling in the MS Windows operating system.<p><a href="https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;debug&#x2F;about-structured-exception-handling" rel="nofollow">https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;debug&#x2F;about-s...</a><p><a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Microsoft-specific_exception_handling_mechanisms#SEH" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Microsoft-specific_exception_h...</a><p>Note that in Microsoft C++, this uses special <i>__try</i>, <i>__except</i> and <i>__finally</i> keywords, which use underscores because they are extensions, separate from the ISO C++ <i>try</i> and <i>catch</i>.<p>SEH can catch events like access violations. It&#x27;s more like Common Lisp exception handling in that you can catch errors without any stack unwinding taking place, so that fixing up a fault and re-starting an instruction is possible (like catching a SIGSEGV or SIGBUS on POSIX).
bvrmnover 5 years ago
Error propagation is must have for any project. You always need to handle some errors in parent context because local knowledge can be not enough. And you can choose between manual passing of error codes (c, go, mundane and boiler-platy), algebraic errors (rust, zig, optional sugar can make it convenient and look like SEH) and SEH. Yes it is additional condition flow, but you have to introduce it in some way.
fuzzy2over 5 years ago
Didn’t Java require all exceptions be declared in the method signature? (Haven’t used it in years.)<p>I think that’s the best solution. You declare what error conditions could arise, but not using magic numbers. The caller can then decide whether to handle them or pass them to its caller etc.<p>I agree that unexpected exceptions (like it’s possible in C# and possibly others, you can just throw whatever whenever) are far from optimal.
评论 #22167285 未加载
评论 #22167294 未加载
评论 #22167299 未加载
ensiferumover 5 years ago
The problem comes when programmers muddle and get confused about different possible &quot;failure&#x2F;error&quot; conditions in a program.<p>A) Bugs, i.e. errors made by the programmers of the software B) Expected logical conditions that only appear as &quot;errors&quot; to the <i>users</i> C) Unexpected failures such as resource allocation failures<p>Let&#x27;s have a look at these one by one.<p>Case A, i.e. bugs written by programmer, i.e. the program is about to violate it&#x27;s own constraints&#x2F;logic&#x2F;pre&#x2F;postconditions&#x2F;invariants. Examples are many, but for example off by one, accessing nullptr&#x2F;null object, array out of bounds etc. Really the best way to deal with these is to abort&#x2F;panic&#x2F;dump core with a loud bang and produce a core dump and a stack trace to simplify debugging.<p>Case B, i.e. conditions that the application should expect to encounter and be prepared to deal with. For example your TCP socket connection times out, DNS query fails, HTTP query response returns 404. In any such case when you need to write some logic such as &quot;retry to open the sockect connection&quot; or &quot;tell the user that the resource was not foudn&quot; or try to resolve DNS query again later the best method is to use some kind of error enum&#x2F;number&#x2F;code kind of system. And to recap the &quot;errors&quot; in this category are not errors for the software but only errors for the <i>user</i>. I.e. for the software these are just logical conditions.<p>Finally Case C), errors that are neither A or B. Typically these are just some very unexpected conditions such as resource allocation failures. Your program failed to allocate a socket or pipe or a mutex or whatever. Should not happen but <i>might</i> happen once in a blue moon under heavy system load or unexpected conditions. These are the things you don&#x27;t want to conflate with your logical &quot;error condition&quot; path from case B because&#x27;t that won&#x27;t scale and propagation of errors up the stack becomes very tedious. These are best suited for exceptions.<p>Edit: Note that this isn&#x27;t a static state but what is considered a bug or a logical error <i>can</i> change across modules depending on the software project organization. A module written by another team migth consider illegal arguments as &quot;Logical error conditionds&quot; and return error codes, whileas a similar module written and used within a single team might just consider this a bug and dump core.
评论 #22168951 未加载
ch_123over 5 years ago
&gt; Dealing with them in a more elegant way than inelegantly crashing goes all the way back to 1962 in a near-forgotten language called LISP.<p>Even as someone who isn&#x27;t a Lisp programmer, I find the idea that Lisp is a &quot;near forgotten language&quot; to be a bit of a stretch.
javajoshover 5 years ago
I agree that `throw` is easy to misuse; but I think this is a symptom of something else, namely in architectures where input even handler code is spread all around or, worse yet, dynamic. This is really common in many languages, and perhaps my greatest pet peeve.<p>However, throw is a wonderful facility if your architecture has one defined entry point for inputs of all kinds. It means your outermost loop can have a try block, and literally everything else can feel free to throw in the safety and comfort of knowing the entire program won&#x27;t fall down.
评论 #22167754 未加载
playpauseover 5 years ago
Can someone point me to a code example (preferably TypeScript or JS) to clarify what the author proposes as an alternative to ‘throw’? Maybe it’s Stockholm Syndrome, but I’m struggling to picture how returning an error code (and checking it with an if-statement after every function call) would make code easier for a human to analyse. Seems like it would just make it a lot noisier and less expressive. But I’m intrigued.
lilyballover 5 years ago
The keyword, and exceptions, are orthogonal concepts that this post is conflating. The real issue this post has is with exceptions (and in particular user-defined exceptions, though I&#x27;m not really sure why this matters because you can always (ab)use system exceptions if you have a way to reliably trigger them). You can still use the `throw` keyword with other error-handling models, such as with Swift&#x27;s Errors.
foreignerover 5 years ago
Could some sort of hybrid approach be an improvement? What if we required every function to explicitly declare the types of exceptions it might throw? The compiler could enforce it and make it transitive to higher level functions.
评论 #22167630 未加载
评论 #22167405 未加载
lwhiover 5 years ago
When &#x27;pair programming&#x27; was chucked in as an example of one of the worst things that happened to programming I lost interest.<p>Back up your assertions with facts. If you don&#x27;t have facts use good truthful examples.<p>Too much opinion and conjecture isn&#x27;t useful.
评论 #22167516 未加载
评论 #22168861 未加载
classifiedover 5 years ago
Publishing on hackernoon is a mistake. The site sucks and also disables the Reader View that would make it tolerable.
评论 #22167642 未加载
altitudinousover 5 years ago
throw is being used as a replacement for goto.
评论 #22167148 未加载
评论 #22167130 未加载
评论 #22168987 未加载