3 ms·
I haven't heard anyone anywhere say that "typed exceptions are an antipattern" just "typed exceptions _as implemented by Java_ are an antipattern". The general
by svieira 2y ago
I haven't heard anyone anywhere say that "typed exceptions are an antipattern" just "typed exceptions _as implemented by Java_ are an antipattern". The general problem is well set forth by Anders Hejlsberg, Bill Venners and Bruce Eckel in _The Trouble With Checked Exceptions_ [1]. Briefly, typed exceptions in Java are not genericizable. Everything must be known at compile time. That is to say, you cannot write:
interface ThrowingRunnable<E extends Throwable> {
void run() throws E;
}
Instead, the best you could do is manual multiplication of interfaces:
interface AThrowingRunnable {
void run() throws ExceptionA;
}
interface BThrowingRunnable {
void run() throws ExceptionB;
}
// ... snip thousands more ...
Or give up and erase all the type information by saying `throws Exception` or `throws Throwable`.
Genericizing throws in particular was tried in Midori [2] and worked out really well (by report). In addition, several less-than-completely-obscure languages are starting to experiment with the algebra of effects in general (as opposed to error handling in particular). Pony [3], OCaml [4], and others are experimenting with bringing what Koka [5] (among others) to the masses.
[1]: https://www.artima.com/articles/the-trouble-with-checked-exceptions https://www.artima.com/articles/the-trouble-with-checked-exc...
[2]: https://joeduffyblog.com/2016/02/07/the-error-model/ https://joeduffyblog.com/2016/02/07/the-error-model/
[3]: https://www.ponylang.io/ https://www.ponylang.io/
[4]: https://ocaml.org/manual/5.2/effects.html https://ocaml.org/manual/5.2/effects.html
[5]: https://koka-lang.github.io/koka/doc/index.html https://koka-lang.github.io/koka/doc/index.html
- sc22 2y agoYou _can_ write in Java that very interface you claimed could not be written. interface ThrowingRunnable<E extends Throwable> { void run() throws E; } Only, hardly anybody writes it that way, so people forget that it's possible. Why don't people use exception polymorphism in that way? I am not sure, but I believe it would make code too verbose, since every function containing a virtual method call to something that might throw would have to be parameterized that way.