Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

In other words: Footgun #17421 Exhibit A.


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"; }


Why is this a footgun? This seems no different than using a try/catch.

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


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.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: