3 ms·
> I think checked exceptions are backwards. If you use a `throws` declaration the caller must catch it. I'm not sure I I'm reading your comment right, but thi
by lultimouomo 6y ago
>
I think checked exceptions are backwards. If you use a `throws` declaration the caller must catch it.
I'm not sure I I'm reading your comment right, but this is plainly false. Caller can add a throws declaration themselves and catch anything.
It seems to me that what you're advocating bears no difference at all with checked exceptions. The "unsafe" escape hatch is called RuntimeException. "throws" behaves exactly like your advocated "onlythrows".
The only actual difference you're proposing, AFAICS, is that you'd like java to handle nulls differently - and I think we can all agree on that.
- captainmuon 6y ago> Caller can add a throws declaration themselves and catch anything. You are right. I meant someone in the call stack has to catch, not the immediate caller. That I mentioned null pointers is a red herring. I want to add a block that tells the compiler "prove that no exceptions (checked or unchecked, not even NPE)" can escape outside of this block!". The escape hatch is about this: imagine you have a function that throws if the argument is odd, but you know you will only call it with an even number: void myFunc(int i) never_throws { swear_never_throws(NumberOddException) { throwsIfOdd(i*2); } } (Apologies for the pseudo-code, I haven't made up a nice syntax.) If you mark your function that no exception can escape (not even an unchecked exception!), but the compiler sees that throwsIfOdd can throw NumberOddException, you must of course assert that what you are doing is OK.