Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Checked (and unchecked) exceptions create an exponential number of control flow paths. Consider:

   try {
      throws_a();
      r = grab_resource();
      throws_b();
      r.throws_a();
   } catch (a) {
      r.release(); // oops; null pointer some times
   } catch (b) {
      try {
        r.release();
      } catch (a) {
        // nooo....
      } 
   } finally {
     if r != null {
        r.finalize() // use after release (sometimes)
        r.release() // ???
     } 
   }
There is plenty of academic literature showing that real programs are even worse than my contrived example, on average.


The construction of arbitrary convolutions of logic is not demonstrative of your assertions. A multitude of code paths is not specific to checked exceptions. Note: It is possible to try catch a union of exception types in the recent versions of Java (catch X | Y | Z).

The paths are still there if the exceptions are all unchecked or combined or not using exceptions at all. Passing exceptions up is rarely the right choice, imo. If it's a terminal runtime, sure, obv.


> r.release(); // oops; null pointer some times

Quite sure Java would prevent that if r was never assigned.

Also most languages provide cleaner try-with-resources or RAII style lifetime management so you don't actually end up with that kind of spaghetti code unless you actively go out of your way to be bad at programming.


> Also most languages provide cleaner try-with-resources or RAII style lifetime management so you don't actually end up with that kind of spaghetti code unless you actively go out of your way to be bad at programming.

Context managers (try with resources) specifically don't work at all when release is conditional e.g. open a file, need some post processing, need to close the file if the post-processing fails but return it if it succeeds.

Defers don't either, unless you have a specialised one (e.g. zig's errdefer).

Actual RAII (aka affine types) does work, but adding that to a GC'd langage after the fact is a huge undertaking and increase in complexity, because the interaction between affine and normal types is fraught.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: