Live data from Hacker News

My philosophy of exceptions: they're always ambiguous (2021)

adamhooper.medium.com

21–30 of 46 posts

Re: My philosophy of exceptions: they're always ambiguous (2021)

#21
post #11
post #2

> So you think you know what an “exception” means? An exception is when code can no longer do any meaningful work. This could be because of a programming bug, unexpected input, network issue, hardware issue, etc. The cause doesn't really matter. What matters is that there is no way forward for the code. And this is a decision that is made (or not made) by the programmer. In this author's Python example, he chooses ac…

> An exception is when code can no longer do any meaningful work. That is quite obviously not true, not even on the surface. I have written tons of code where I've caught exceptions and then continued to do meaningful work. The author's Python example even contradicts your statement. Sure, maybe they should validate the URL first. But even if they did that, the server could be down, the user's internet could be down,…

The code that catches an exception can continue, but the point was about the code that threw the exception. You throw an exception if you can't continue the work you're currently doing, for whatever reason. Of course, the rest of the program can very well continue: some other part will catch the exception you threw and decide what to do next. Ending the entire process is an extreme case, that should be reserved for only global problems - out of heap space errors, memory corruption, security violations, deadlock detection. Anything else should result in a more localized failure - killing a thread, a task, a request processor etc.

Also, there is little fundamentally different between throwing an exception and returning an error result. Everything I wrote above applies just as much to error results as it does to exceptions. Exceptions are just a language-level mechanism to help with a common error pattern: the code that detects an error condition is typically very far away from the code that can handle that error condition. When an error happens, the vast majority of the time, what happens next is that the error will be cascaded back through the call chain to a handler function of some kind. Along the way, some context will be added to the error, and some resources will be cleaned up. You can do this by hand, as you would in Go or Rust, or you can have the runtime do it for you, like in Java or C#. There are pros and cons to both (implicit vs explicit, how relevant is the context, etc), but fundamentally the program will take the exact same path.

Re: My philosophy of exceptions: they're always ambiguous (2021)

#23
> Here’s the big lie: in Python, this snippet will almost always raise an Exception. That’s because most users, faced with an input prompt, will press Ctrl+C or enter a non-URL. And even if they enter a URL, it probably has a typo.

[citation needed]

> In English, “exception” means, “something unusual.” But if this code snipet raises ValueError 60 per cent of the time and prints the contents of a web page 10 per cent of the time … well, surely we need a new term?

The purpose of this code snippet is to download a web page and show it to the user. The code considers situations that lead to it not fulfilling its purpose “exceptional”. If someone exits when they see a prompt for a URL, or type gibberish, why did they run a program to download a web page in the first place?

Re: My philosophy of exceptions: they're always ambiguous (2021)

#24
post #18

I think this topic has been discussed countless times, and this post fails to add anything worthy to it. My two cents: use result types, or their analog in your language for expected error conditions, and use exceptions for exceptional situations. Example for the former is parsing, which has an expected failure mode - something couldn’t be parsed as intended. Example for the latter is a network issue during an API ca…

> They let people concentrate on the actual business logic, not sprinkling little bit of business and error handling logic all over the place

This can lead to trouble if the programmer doesn't keep in mind that various (but perhaps not all) operations can throw. In particular, proper behaviour in the exceptional case may require a more specific order of operations than is needed for proper behaviour in the normal case.

Raymond Chen did a great blog post on this in 2005, Cleaner, more elegant, and harder to recognize. [0][1]

[0] https://devblogs.microsoft.com/oldnewthing/20050114-00/?p=36...

[1] https://news.ycombinator.com/item?id=406391

Re: My philosophy of exceptions: they're always ambiguous (2021)

#25
You can have the same philosophical distinction over the term "error". Is it really an error if your computer did not literally catch fire or otherwise took physical damage?

And yet this hasn't stopped even nontechnical people from using the term without any significant communication problems.

I think a more interesting discussion might be what exactly the difference between "errors" and "exceptions" is in computing, though I'm not sure there is one in practice. (I think in theory, "errors" are supposed to be caused by factors external to your program, like hitting memory constraints, while "exceptions" are internal to your program? I'm not sure if this is universally true though or just a java-ism - and even in java it's not consistently followed through everywhere).

So until this is decided, I think the most useful practical definition of exceptions is "it's a special language construct that makes it easier to manage errors".

Re: My philosophy of exceptions: they're always ambiguous (2021)

#26
post #9

>>> When I shifted from Rust to Python, I tried to write matcher-style code. It wasn’t legible. I became irate. so, in other words... rust developer tried to write rust in python, and it didn't work. wasn't that the same story for each combination of two languages? I remember people criticize other people for "always writing java in any language they work with"...

> I remember people criticize other people for "always writing java in any language they work with"...

Apples to oranges. When people say “writing Java in any language…” they mean it usually in a derogatory term, as in overly verbose variable names, AbstractSingletonProxyFactoryBean, etc. When people talk about Rust they usually mean functional style, exhaustive checks, safe constructs, etc.

Re: My philosophy of exceptions: they're always ambiguous (2021)

#28
post #18

I think this topic has been discussed countless times, and this post fails to add anything worthy to it. My two cents: use result types, or their analog in your language for expected error conditions, and use exceptions for exceptional situations. Example for the former is parsing, which has an expected failure mode - something couldn’t be parsed as intended. Example for the latter is a network issue during an API ca…

The main problem I have with exceptions is that libraries can't be trusted to only throw exceptions in exceptional situations, or even properly document the situations in which they throw exceptions, so my code must be constantly paranoid about every call into library code. Contrast this with languages that provide result types and the certainty of either handling or passing up the error, particularly when the language provides a clean construct like ? in Rust - I just really like the feeling of certainty I get when writing that code over code that can throw exceptions.

Re: My philosophy of exceptions: they're always ambiguous (2021)

#29
post #25

You can have the same philosophical distinction over the term "error". Is it really an error if your computer did not literally catch fire or otherwise took physical damage? And yet this hasn't stopped even nontechnical people from using the term without any significant communication problems. I think a more interesting discussion might be what exactly the difference between "errors" and "exceptions" is in computing,…

There was a book “C Interfaces and Implementations” by David R. Hanson and he put it so: there are user errors (input/data errors), program bugs (things we 'assert') and everything else are exceptions. So a non-existent file is a user error, uninitialized memory is a program bug, and an arithmetic overflow is an exception. Not sure if this is useful.

I myself think errors and exceptions are misleading terms. There is an instruction; it does this and that and can produce the following results. We call some of there results errors because usually we imply a goal. But instructions do not really have goals. We search for a key in a dictionary and either find it or not. Either result can be an error or not an error depending on whether we expect the key to be there or want to make sure it does not exist. Yet the searching instruction is same. There is no reason to prefer one result or another. It is more important to make all the necessary distinctions.

Re: My philosophy of exceptions: they're always ambiguous (2021)

#30
> If you’re using Python, you’re stuck with the endless debates. For instance, you’ll never know why there’s a dict.get() to avoid KeyError but no list.get() to avoid IndexError.

Probably for the same reason that, in Swift, dictionary lookup returns an Optional but Array traps on out-of-bounds: dictionary keys tend to come from outside the dictionary, but array indices tend to be derived from the array bounds. This means a dictionary lookup failure tends to be expected, and an out-of-bounds array index tends to be a logic error. More details here: https://stackoverflow.com/a/75780575/77567

Post reply on HN