Skip to main content


Hey, fellow software developers! Sometimes code passes your code bad data. Do you throw exceptions? Return false? Hope for the best? (YOLO)

Would love to hear your reasons why. And please share so other developers can weigh in.

#Developers #Software #SoftwareDevelopers #OpenSource #DivideByZeroAndConquer #Perl #Ruby #Python #Java #CSharp #NodeJS

  • Exceptions (71%, 81 votes)
  • Return values (23%, 27 votes)
  • YOLO (5%, 6 votes)
114 voters. Poll end: 2 weeks ago

in reply to Curtis "Ovid" Poe (he/him)

it depends, I'm a data scientist so my code gets sent a lot of "bad" data. Sometimes it is a sign of improperly configured read mechanism (character encoding, CSV dialect are favourites). Sometimes your source does not fulfil your expectations (supplies a None when you expected an int) - this might happen millions of rows into a data source. In these cases I'd handle the issue.

#datascientist

in reply to Ianhopkinson

@ianhopkinson Still remember building an ETL system where five pharmaceutical companies signed contracts with us, all agreeing to a stringent data format for what they were sending us.

Not one of them kept that agreement.

in reply to Curtis "Ovid" Poe (he/him)

working with open data can be even worse - since there is no agreement they can vary formats arbitrarily!
in reply to Curtis "Ovid" Poe (he/him)

Too general of a question. What context? What is error recovery like in general in the system?
in reply to Elliot Shank

@clonezone Agreed. There are times you want different approaches (die, complain, ignore), but I only have so many characters for a toot.
in reply to Curtis "Ovid" Poe (he/him)

I tend to return where the operation is low value and not important to the application, and use exception handling where the operation might cause a crash; but it's definitely situational and depends on how critical the application is and what language it's written in - some have better exception handling than others.
in reply to Curtis "Ovid" Poe (he/him)

i voted “exceptions”, because i favor the let it crash philosophy, but ill do values if it makes sense, but only if it makes sense

i get eternally annoyed by code that “handles” exceptions because “that’s what good code does”, without actually adding any value, and in a lot of cases hiding problems making it impossible to troubleshoot

in reply to Curtis "Ovid" Poe (he/him)

I mostly opt to use return values and keep exceptions for "exceptional" situations and internally.

However like so many other things in this industry it depends on the specific situation, such as data source, data format, what it's used for and is it internal or externally provided etc.

in reply to Curtis "Ovid" Poe (he/him)

For me it totally depends on the context. When I'm doing exploratory work, I go for broad exceptions and other fail-fast approaches that quickly bring everything to a halt. When I'm looking for a stable production system, it's a mix of narrowly contained exceptions with good error management and defensive coding such that return values are, from a domain perspective, sane, so the system can keep on keeping on.
in reply to Curtis "Ovid" Poe (he/him)

If the bad data is coming from outside my control and it’s not something I necessarily anticipated then I would prefer an exception thrown and aggregated somewhere for better visibility.

If that specific case keeps happening often enough as to no longer be considered surprising, I may eventually codify it and make a special return value for it.

in reply to Curtis "Ovid" Poe (he/him)

I would throw exceptions. You can customize exceptions and handle them differently depending on the case (die, warn or ignore). But I would agree it depends on the context and value of the effort to use one or the other
in reply to Curtis "Ovid" Poe (he/him)

I thought it is pretty dry and clear by now. If an algorithm has an result, then you pass the result. If the result can not be achieved, you have an exception. If an external source delivers invalid data, you validate it before you continue processing - if you fail the validation via exception or other means is up to the API. That's pretty much the agreed standard for decades now, or am I incorrect? 🙂
in reply to Curtis "Ovid" Poe (he/him)

While I do prefer a result value (Either monad or similar), most often I end up using exceptions, mostly because of consistency with existing code.
in reply to Curtis "Ovid" Poe (he/him)

I'm in the 'it depends' crowd. If it's suspect, but I can live with it, it gets logged and I move on. If it's completely garbage, it gets logged and ignored. The only time I have exceptions is when someone needs to look at the data and decide what to do in this specific case.

I had one like this recently -- "Really? This script's been in service for six years, running every half hour, and that's the first time we've seen this error?" I implemented a fix, and things started up again.

in reply to Curtis "Ovid" Poe (he/him)

it depends. I work on systems and tooling so sometimes it’s bail or YOLO, but with my PLs of choice and requirements it usually means a return value 😀
in reply to Curtis "Ovid" Poe (he/him)

Either handle the error at the point it happened, or return a specific subclass of Result<V, E> so the caller - which has more context - can decide how to handle the error.
in reply to Curtis "Ovid" Poe (he/him)

Exceptions, by being nonlocal and hard to ignore, tend to be the safest bet. Also easy to add to new situations that didn't previously support it. Doesn't get in the way of return-based data flow.
in reply to Curtis "Ovid" Poe (he/him)

Exception generation & stack unwind is much less efficient than return-value checking; so if errors are expected to be frequent, I opt for return values. If errors are expected to be rare, I prefer exceptions, because RAII and stack unwinding make resource management so much safer
in reply to Curtis "Ovid" Poe (he/him)

IMO input data should raise an exception so that custom code behaves similarly to how the core behaves.
in reply to Curtis "Ovid" Poe (he/him)

that’s such a “there be dragons” question 😄

I tend to always throw exceptions when receiving bad data from an initialization-like call, but usually do false returning internally for little stuff.

But if I have to hand data back away from my stuff I’ll throw an exception too if something goes wrong handing it back before exiting.

If it’s something complex where I can be confident exceptions are handled I’ll use them everywhere. There’s a bit of a twisted freedom in that.