logoalt Hacker News

josefxtoday at 3:49 PM1 replyview on HN

What the blog doesn't mention is how try finally can mess up your control flow.

In Java the following is perfectly valid:

try { throw new IllegalStateException("Critical error"); } finally { return "Move along, nothing to see here"; }


Replies

reactordevtoday at 4:02 PM

Yes, Java has footguns too.

The existence of two different patterns each with their own pitfalls is why we can’t have nice things. Finally shouldn’t return a value. Simply a void expression. Exception driven API’s need to be snuffed out.

If your method throws, mark it as such as force me to handle the exception if it does, do not return a non-value value in a finally.

Using Java as the example shows just how far we have come with this thinking, why old school Java style exception handling sucks and why C++ by proxy does too.

It’s difficult to break old mental habits but it’s easier when the compiler yells at you for doing bad things.