Hacker News new | ask | show | jobs
by circlingthesun 3293 days ago
"no way to know which exception you are gonna get"

Java much?

4 comments

I don't know about OP but you don't even need to go to the level of checked exceptions, with their pain.

In Python, you are encouraged to follow Easier-to-Ask-Permission-than-Forgiveness and catch specific exceptions. Doing either / both requires you actually know what exceptions to catch so you don't hide bugs. The problem is that the docs rarely mention the expected exceptions. You have to resort to discovering them experimentally and hope that that exception was an intended part of the API contract and not an implementation detail.

IMO package authors should document the exceptions that they throw.

The Python exception hierarchy is not as good as I'd like. It should be harder to match NameError, AttributeError, etc because there's rarely a sane way to handle those.

People often write code expecting to "handle" exceptions but usually end up masking design defects. Most of the time you can only sanely handle a handful of exceptions way back at the base of the stack. And the handling at that level is usually "log" and/or "limited backoff/retry mechanism". Most of the code you're writing shouldn't do much with exceptions other than perhaps add context and re-throw.

Not to mention old craft like old style exceptions that for some reason hasn't been cleaned out. Python always choose strict backwards compatibily instead of creating aliases to mimic the old behavior with new standard behaviour which make certain old libraries a minefield.
Java too little?

Even if you're not required by the compiler to handle an Exception, knowing which ones are possible to be thrown in a part of the code is still handy.

The same way Optionals and Enums in language like Rust force you to handle all cases.

I was under the impression that every method had to declare which exceptions are thrown via the `throws` keyword. Is that not the case?
Only for checked exceptions; a RuntimeException does not need to be declared.
Correct, all checked exceptions are declared in the method signature.
That's the opposite of the spectrum.