2 ms·
I prefer unchecked exceptions, because I prefer "crash it" approach to software design. Most of errors should crash my program (or part of it), write detailed l
by vbezhenar 6y ago
I prefer unchecked exceptions, because I prefer "crash it" approach to software design. Most of errors should crash my program (or part of it), write detailed logs (including stacktraces) and then it should be restarted, continuing its operation. If it's request-handling system, current request should be aborted.
With this approach, error handling is pretty minimal. All I have to think about is exception-safety. And that's an easy thing to reason about, when language does not do manual memory management.
I understand that this approach is not suitable for every software field. If I'm writing martian rover software, I don't want to crash my software, when rover might be physically destroyed.
Of course some exceptions are not exceptional and they are part of normal flow. But those situations are rare and try-catch is perfectly fine to handle those errors as well. I'm not forced to handle them, that's right. But even if I did not think about it, I'll get crash, I'll get logs, I'll get user complaints and I'll implement that separate handling. Probably it's corner-case anyway, so that's not a big deal.
And a big plus is that I don't have to think about exceptional situations when I'm writing my code. That reduces cognitive load and allows me to focus on main logic, rather than thinking about every possible outcome of every called function. And main logic is why I'm writing a software in the first place.
Unchecked exceptions are untyped indeed. They are like JavaScript on top of typed language. Well, I'm not strict-type purist. I'd use Haskell if I were one, I guess. Dynamic typing is popular for a reason. May be it would be an interesting project to implement strictly-typed exception handling. But Java did it wrong. I don't know how to make it right. If someone would make strictly-typed exception handling natural and friction-less to use, I'm all for that. But I did not see that kind of a system anywhere. And untyped works for me good enough.
- thayne 6y agoBut that is part of the appeal of errors as part of the return. Recoverable errors are just return values and can be handled with normal code, and irrecoverable errors panic.
- ben-schaaf 6y agoThat's pretty much exactly what Rust does. You sprinkle unwrap where you don't care or don't expect failure and it will crash there (with a backtrace), otherwise you can use the better error handling semantics of Result to properly handle the errors you care about.