4 ms·
Java’s Checked Exceptions Are Just Weird Union Types
- jfengel 4y agoSure. Anybody who's ever written in an exception-less language has created those "ValueOrError" types, and then dealt with the confusing morass of if-else statements. They're "just" union types, plus a syntax that puts the happy path of your code front and center. It's not mandatory in a language. It's a convenience for programmers to draw a strong distinction between the primary path of the code and the billions of ways that it can go wrong. Checked exceptions are a compromise between putting the error condition on a conceptual par with the success condition, and completely ignoring the possibility of error (the way unchecked exceptions allow you to do). That middle position lets the compiler help you out: you need to acknowledge that the error is a possibility, and you can either just let it cascade up or catch it and try to recover. So yeah, that puts it in the same conceptual position as the return type of the code.