3 ms·
> exceptions are used as routine flow-control Unfortunately, this approach is encouraged by some popular frameworks, for example a ResponseStatusException in Sp
by xkr 8y ago
> exceptions are used as routine flow-control
Unfortunately, this approach is encouraged by some popular frameworks, for example a ResponseStatusException in Spring 5
- bunderbunder 8y agoAnd even the JDK itself. java.lang.InterruptedException comes to mind.
- xxs 8y agoInterruptedException is sort of necessary evil, esp. when it comes to concurrent data structures. InterruptedException are rare (very) and used mostly to signal termination attempt. LockSupport.parkNanos doesn't throw the exception (unlike sleep, and sleep should never be used in code) but the interruption signal/flag has to be respected somehow. Most of the time is rethrow in library code or bailing out with IllegalStateException in 'userspace'
- bunderbunder 8y agoI think that it could be done better. .NET's CancellationTokens are a step in the right direction, though sadly just one step. You can check if cancellation is requested, but there's also a method for asking the token to throw a CancellationException if cancellation has been requested, and this is how all the abstractions around threading do it. But it's reasonably easy to imagine ways to produce an API that only uses tokens like that instead of using exceptions to signal cancellation/termination requests. And I suspect, given how many bits have been spilled trying to explain to people just how they're supposed to deal with InterruptedExceptions, that the resulting system would have better ergonomics. Long story short, I don't know that it's that InterruptedException was a necessary evil so much as that people were still fairly new to this whole exceptions thing when Java came out, and it's really hard to get subtle things like this right on the first try.