Hacker News new | ask | show | jobs
by dfe 464 days ago
I don't agree with the criticism of Java's exception handling, since Java literally does make exactly the distinction between "1. A Bug in the system" as RuntimeException, and "2. A faulty situation that can't be avoided" as all other Exception, particularly IOException.

Although the same statement is used to catch both, you only catch both if you catch Exception. If you catch IOException or whatever other exceptions you need to catch and you opt to handle them, then RuntimeException will still propagate.

It is only a matter of understanding that this is the distinction, and writing the bodies of your catch blocks accordingly.

And if you are only writing a prototype, declare that it throws Exception, or if you can't, a catch (IOException ex) { throw new UncheckedIOException(ex); } really isn't that bad.

1 comments

In practice people catch runtime exceptions all the time so it's treated more as a recoverable error than a bug. Java let's you recover from runtime exceptions as easy as checked exceptions which blurs the distinction.

> And if you are only writing a prototype, declare that it throws Exception, or if you can't, a catch (IOException ex) { throw new UncheckedIOException(ex); } really isn't that bad.

The problem with that is when you go back to do proper error handling you can't easily find all those places where you did this. For instance in Rust, you can find those by searching for `unwrap`.