3 ms·
Java isn't generic over exceptions. You can't write a method that takes an instance of Foo and says "my method throws whatever Foo.bar() throws" or even "my met
by orthoxerox 4y ago
Java isn't generic over exceptions. You can't write a method that takes an instance of Foo and says "my method throws whatever Foo.bar() throws" or even "my method throws iff Foo.bar() throws".
And this means that your method either always demands to be wrapped in a try-catch, or you migrate to unchecked exceptions.
Rust makes errors a part of the regular type system, so they automatically benefit from all its features.
- mike_hearn 4y ago> You can't write a method that takes an instance of Foo and says "my method throws whatever Foo.bar() throws" or even "my method throws iff Foo.bar() throws". If you could it'd mean altering the implementation would automatically alter the API, which would be rather unexpected. That's why the Java approach is to wrap exceptions and propagate causal chains. The underlying errors thrown by the implementation can change but the advertised exceptions don't, but no information is lost.
- auno 4y ago> Java isn't generic over exceptions. You can't write a method that takes an instance of Foo and says "my method throws whatever Foo.bar() throws" If you bend over far enough backwards, and squint a bit, you can kind of do that... interface Runner<E extends Throwable> { public void run() throws E; } class Test { public static <E extends Throwable, T extends Runner<E>> void test(T runner) throws E { runner.run(); } } It compiles. I haven't tried running it though.