3 ms·
I mean, can't you just catch Throwable (base class) and be done with it?
by TedDoesntTalk 4y ago
I mean, can't you just catch Throwable (base class) and be done with it?
- layer8 4y agoThis forces client code to handle arbitrary exceptions and it loses the information about which specific exceptions are actually thrown. This is like having Object as the return type on all functions, and having callers do instanceof to see what type of result they got. Exceptions should be part of the semantic contract of functions/methods just like return types are.
- zaphar 4y agoExceptions are a return type of functions. They return via a different path but there is no possible way you could disagree that they are a form of return.
- layer8 4y agoExactly my point?
- dllthomas 4y agoI understood it to be intended as agreement with emphasis.
- dllthomas 4y agoAs they don't return to the same place, don't return to a single place, and may (if not caught) not actually return but instead take down the entire thread, I think it's reasonable that for some purposes it might well make sense to treat exceptions as something other than "a form of return." ¯\_(ツ)_/¯ However we're treating them, however, I agree that they are a part of the interface and should be documented (and, where relevant, checked) as such.
- int_19h 4y agoThe point is that the type system doesn't properly reflect that. You should be able to declare a generic method that takes some T, and throws everything that T.foo() throws, for example.