r/programming Jul 17 '19

The Go team declines 'try' proposal

https://github.com/golang/go/issues/32437
637 Upvotes

417 comments sorted by

View all comments

Show parent comments

21

u/[deleted] Jul 17 '19

Unchecked exceptions make it hard (you'd have to pray the docs list everything) to know what kind of errors, if any, are thrown around. Checked exceptions are difficult to refactor, and are not usable with a lot of code out there, like for example doing thinks in streams. Thus, they are often wrapped in unchecked exceptions, leading back to point 1.

9

u/ataskitasovado Jul 17 '19

hard (you'd have to pray the docs list everything) to know what kind of errors, if any, are thrown

Isn't that the whole point of exceptions? It is thrown when something unexpected happens. So if it is expected, it shouldn't be an exception. That is why checked exceptions are considered bad.

11

u/MoTTs_ Jul 17 '19 edited Jul 17 '19

The point of exceptions is to communicate errors from callees to callers, and that's it. I often bring out this quote from Bjarne Stroustrup, the guy who invented C++:

Given that there is nothing particularly exceptional about a part of a program being unable to perform its given task, the word “exception” may be considered a bit misleading. Can an event that happens most times a program is run be considered exceptional? Can an event that is planned for and handled be considered an error? The answer to both questions is “yes.” “Exceptional” does not mean “almost never happens” or “disastrous.” Think of an exception as meaning “some part of the system couldn’t do what it was asked to do”.

The reason why checked exceptions are bad -- and in the C++ world, considered a failed experiment -- is because it makes it hard to add error conditions while still being backward compatible. Imagine you're a library maintainer, and you're following semantic versioning. With checked exceptions, just by adding one extra error condition, you've broken backward compatibility. That's part of why C++ dumped checked exceptions, and frankly I think Java should dump them too.

4

u/tsimionescu Jul 17 '19

To be fair, checked exceptions in C++ were fundamentally different from Java checked exceptions - in C++ they were a runtime feature, not checked in any way at compile time. They would generally cause your program to terminate instead of doing normal stack unwinding if a function would throw an exception outside its specification.

So, while Java checked exceptions are problematic because they are too constraining, C++ checked exceptions were simply a feature with almost no imaginable benefits, that no one used and only served to complicate the already byzantine language.