by orthoxerox · 3y ago · 0 comments
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.
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.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.