Live data from Hacker News

Isaacs: try/catch is an anti-pattern

groups.google.com

21–30 of 140 posts

Re: Isaacs: try/catch is an anti-pattern

#21
> At least JavaScript doesn't have typed catches. Holy black mother of darkness, that shit is intolerable.

I would like the author to expand a bit instead. In python there are typed catches, it seems to make a lot of sense to me: you catch only the exceptions you want, and let the other ones bubble up. It is well explained in Martelli's Python in a Nutshell.

I have seen try/catch construct in Java and Javascript, and it is probably less readable, and certainly can be over-used, but in Python, returning None in all exceptional cases is annoying, different issues got merged into a single "Muted" case.

Re: Isaacs: try/catch is an anti-pattern

#22
post #7

This is the standard in many languages and one that I believe that java got wrong. It was a little rushed with the whole concept of forcing everyone to handle all exceptions concept and building so many exceptions in for silly things like connection failures and parsing errors that reasonably can be expected to happen constantly in the normal runtime of an application. I follow the rule in almost every language that…

Ruby also gets it wrong in several common cases. The IO libraries throw exceptions on expected events, for instance, and there are a couple of places where I flat-out disagree with the Exception class hierarchy.

Re: Isaacs: try/catch is an anti-pattern

#24
> You can't reasonably argue that this:

  try {
    foo = JSON.parse(input)
  } catch (er) {
    return "invalid data"
  }
is more lightweight than:

  foo = JSON.parse(input)
  if (!foo) return "invalid data

This is like saying that walking is faster then driving because I can walk 5 meters faster then it takes me to get into a car. Yes, error codes are more compact in a tiny "Hello world" example because it is only showing one function call. Exception handling becomes more compact when you're writing something less trivial and you don't have to repeat the same error handling code after every call.

> Try/catch is goto wrapped in pretty braces. There's no way to continue where you left off, once the error is handled.

Don't throw exceptions if you can handle the error and continue where you left off. Exceptions is for when you can't continue. Think of throwing as a way to roll back transaction, stop whatever you were trying to do, and go back to the last consistent state.

Obviously, exceptions are not perfect. As author correctly notes, they require careful consideration of what's exception and what's part of normal flow. But they were invented for a reason, and I don't see the article offering any alternative solutions to the problems that exceptions are solving now.

Re: Isaacs: try/catch is an anti-pattern

#26
post #18
post #7

This is the standard in many languages and one that I believe that java got wrong. It was a little rushed with the whole concept of forcing everyone to handle all exceptions concept and building so many exceptions in for silly things like connection failures and parsing errors that reasonably can be expected to happen constantly in the normal runtime of an application. I follow the rule in almost every language that…

> It was a little rushed with the whole concept of forcing everyone to handle all exceptions Just to clarify, Java doesn't force you to handle all exceptions (maybe it did at one point in time?). Exceptions which inherit from RuntimeException are unchecked and you can choose whether to handle them or not. Exceptions which don't inherit from RuntimeException are checked

That being said, I'd like to quote these passages from the official Java tutorial:

"Generally speaking, do not throw a RuntimeException or create a subclass of RuntimeException simply because you don't want to be bothered with specifying the exceptions your methods can throw.

Here's the bottom line guideline: If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception."

Anyway... you don't need to neccessarily handle exceptions even if they are checked (in cases where it doesn't make sense that your code handles them). Add a 'throws' clause and let the calling code handle them.

So the poster of the grandparent comment really got it wrong when it comes to Java and exceptions.

Re: Isaacs: try/catch is an anti-pattern

#27

The core of the post: try/catch, which blurs the line between errors that are *mistakes* (accessing a property of null, calling .write() on a stream after .end(), etc.), and those which are expected application-level problems (invalid data, file missing, and so on). Totally agree.

In a typed exception system, you just assign different kinds of exceptions to all of those. Java also distinguishes between checked exceptions ("known unknowns") which need to be declared, and runtime exceptions ("unknown unknowns"). Sure, you can still catch all of them in one place, but that's very rarely a good idea.

A non-typed exception system (that doesn't solve the problem with another mechanism) just sounds like a very bad idea. Is Javascript like that?

Re: Isaacs: try/catch is an anti-pattern

#28

The core of the post: try/catch, which blurs the line between errors that are *mistakes* (accessing a property of null, calling .write() on a stream after .end(), etc.), and those which are expected application-level problems (invalid data, file missing, and so on). Totally agree.

It totally depends on your Application. I wouldn't call a non existing file referenced by a db record, an strange log entry or a malformed xml returned by a webservice an "expected application-level problem". There is absolutely no way for a library writer to decide between an expected and an exceptional condition.

Re: Isaacs: try/catch is an anti-pattern

#29
> At least JavaScript doesn't have typed catches. Holy black mother of darkness, that shit is intolerable.

Well, if his complaint is about untyped catches, well, I guess they must suck. But if he thinks that typed catches are somehow worse (hint: they're better), he is wrong.

> But still, nothing is as bad as the common "On Error Resume Next" that so many terrible VB programs start with.

Ok, everyone who always checks the return value of printf() in C, raise your hands. That's what I thought. The semantics of C is that whenever something fails, you just continue from the next line (statement, whatever...) even if the world is burning. Try-catch may suck, but checking for return values sucks more (in languages without pattern-matching), deal with it.

Re: Isaacs: try/catch is an anti-pattern

#30

This part really sunk in for me in Bruno Jouhier's reply: > "So the big mistake I've always seen people make is being too "nervous" about exceptions and feeling that they have to do something about them as close as possible to the point where the exceptions were raised. They need to the exact opposite: feel relaxed about exceptions and let them bubble up." Hadn't really thought about it in that way, but I find myself…

> "So the big mistake I've always seen people make is being too "nervous" about exceptions and feeling that they have to do something about them as close as possible to the point where the exceptions were raised. They need to the exact opposite: feel relaxed about exceptions and let them bubble up."

While the principle seems to make sense, I find it unusable with GUI like apps. GUI have hundreds of entry point from various events. If I want to catch exceptions far from their raise I need to do it in each one of those event handler, so that they don't bubble through librairies, literally hundreds of times.

Every beautifully written code I've seen has always been CLI programs. Exceptions probably works for server code where you can crash your single process and/or redo it from your original request. GUI and frameworks apps can't really use crash as an acceptable behavior.

Post reply on HN