<p>In Java, checked exceptions are well-known to be <em>extremely</em> unergonomic, to the point that many people circumvent them.</p>
<p>Evidently, checking exceptions, if done properly, can lead to a more robust code—which is an excellent advantage. Rust's approach, though doesn't make use of exceptions, shows this robustness improvement.</p>
<p>Are checked exceptions inherently painful? What designs could be used to make them more ergonomic?</p>

<language-design><exceptions>
Are checked exceptions inherently inconvenient? How to design them in a more ergonomic manner?
<p>There's <a href="https://stackoverflow.com/questions/613954/the-case-against-checked-exceptions/16503128#16503128">an excellent answer</a> by Daniel A.A. Pelsmaeker over on our big sister site, which I'll summarize here.</p>
<p>In it, he classifies the &quot;things that can go wrong in your program&quot; into, broadly, four categories.</p>
<ul>
<li><strong>Fatal exceptions</strong> are stop-the-world. These are &quot;the operating system ran out of memory&quot; or &quot;somebody literally pulled the plug on the computer in the middle of my program&quot;. You cannot stop these. They're not your fault. You can't handle them. You probably can't even log the exception in some cases.</li>
<li><strong>Boneheaded exceptions</strong> are bugs in your program. You accidentally accessed an array index out of bounds, or you passed a negative integer to a function that only works on positive ones. That's your fault, and its your job to prevent that.</li>
<li><strong>Vexing exceptions</strong> are things that happen in the normal course of programming. Trying to parse an integer can inherently fail, and that throws a <code>NumberFormatException</code>. A <code>NumberFormatException</code> doesn't mean your program is flawed. It just means the data you gave it (which may have been user input or something over the network) wasn't what you expected it to be.</li>
<li><strong>Exogenous exceptions</strong> are not bugs in your code, but you have to deal with them. The difference between exogenous and vexing is that the former is exceptional, whereas the latter is something that normally happens. If you try to access a file and it doesn't exist, that's an exogenous exception. The file <em>should</em> exist, but something out of your control made it not so. You have to deal with the problem, though; you can't simply read from a file that doesn't exist.</li>
</ul>
<p>In an ideal API, vexing exceptions are not exceptions. As I described above, they're not actually exceptional. They happen all the time, and it's normal. That <code>parseInt</code> function should probably return an <code>Optional</code> or (in a language like Kotlin with checked nulls) a <code>null</code> on failure. If you need to provide more error information, maybe a Rust-style <code>Result</code> is in order.</p>
<p>So if we're talking about an ideal world, we can assume vexing exceptions don't happen. Fatal exceptions should never be checked (and, indeed, they're unchecked in Java). They're difficult (and sometimes impossible) to catch correctly, so 99.999% of programs should just let the whole thing fail. Boneheaded exceptions also shouldn't be checked. A boneheaded exception is your fault, as the user of the API. You did something wrong and violated a precondition, and the library author was nice enough to check for that and let you know. You don't catch-and-suppress a boneheaded exception; you fix the code that caused it<sup>[1]</sup>.</p>
<p>That leaves exogenous exceptions. These make sense as checked exceptions. Bad things happened that aren't our fault. If they occur, we're in error recovery mode, and we're <em>definitely</em> not in the normal &quot;happy path&quot; of our program anymore.</p>
<p>So I think, broadly, the problems with Java's checked exceptions are:</p>
<ul>
<li>Inconsistent treatment of vexing exceptions. <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html#parseInt-java.lang.String-" rel="nofollow noreferrer">Parsing an integer</a> can throw an unchecked parse error, but <a href="https://docs.oracle.com/javase/8/docs/api/java/net/URI.html#URI-java.lang.String-" rel="nofollow noreferrer">parsing a URI</a> is checked.</li>
<li>When you have exogenous exceptions, they tend to get lumped together like the infamous <a href="https://docs.oracle.com/javase/8/docs/api/java/io/IOException.html" rel="nofollow noreferrer"><code>IOException</code></a>, which is declared in the <code>throws</code> clause of, let's conservatively say, 40% of the Java standard library. I might be able to handle <code>FileNotFoundException</code> specifically, but there's no meaningful way to respond to a <code>throws IOException</code> other than &quot;catch and suppress&quot;, which is always a problem.</li>
<li>This was mentioned in other answers, but exceptions make higher-order functions difficult in Java. A function that takes another function (or any vaguely callable object) as argument must decide up-front whether or not the inner function can throw checked exceptions. That means a lot of functions end up <a href="https://journal.stuffwithstuff.com/2015/02/01/what-color-is-your-function/" rel="nofollow noreferrer">in the red camp</a> and just assume their inner functions can <code>throw</code> whatever.</li>
</ul>
<p>The first bullet point can be solved by replacing vexing exceptions with something viable in the large. The last two can be solved with a better exception hierarchy and more fine-grained control over function effects. Scala is <a href="https://github.com/lampepfl/dotty/blob/release-3.1.0/docs/docs/reference/experimental/canthrow.md" rel="nofollow noreferrer">currently experimenting with</a> a system of effects for checked exceptions.</p>
<p>Now you might wonder why I consider <code>Result</code> different from exceptions. After all, I said &quot;vexing exceptions shouldn't exist&quot; immediately followed by proposing a feature very similar to them. The difference is in granularity. Exceptions, at least Java-style ones, are statement-based. That is, when an exception fires off, the entire current statement stops, and at minimum, something somewhere else (even if in the same function) catches it. Vexing exceptions are inherently normal. They happen all the time, so they shouldn't stop the world and interrupt the normal flow of your program. I don't mind statement-based exceptions for exogenous cases. Those are actually exceptional, so they <em>should</em> stop the world and do something extravagant.</p>
<p>With a Rust-style <code>Result</code>, I choose how to handle the problem inline, as part of the normal flow of control.</p>
<pre><code>let my_number = my_string.parse().unwrap_or(0);
</code></pre>
<p>If we <em>want</em> to propagate the error, we can do so. Rust has the <code>?</code> operator which passes errors up to the caller.</p>
<pre><code>let my_number = my_string.parse()?;
</code></pre>
<p>Or we can assert that the vexing exception is in fact a boneheaded one (e.g., &quot;the caller failed a precondition, or called some unsafe function in the wrong way&quot;), and convert the exception to an unchecked one.</p>
<pre><code>let my_number = my_string.parse().expect(&quot;You did a stupid :(&quot;);
</code></pre>
<p>The point is, the failure state is a real, first-class object in the language, so it can be handled as part of the normal flow of control. Languages with <code>try ... catch</code> <em>expressions</em> like Kotlin move closer to this, but they're still not quite first-class values, so that's why I still advocate for <code>Result</code> types over actual exceptions, at least for vexing exceptions.</p>
<hr />
<p><sup>[1]</sup> When I say &quot;don't catch this&quot;, I'm naturally excluding the case of a top-level <code>try ... catch</code> responsible for logging. A catch-all in your <code>main</code> is generally fine for anything non-fatal and improves your ability to iterate on code. When I say &quot;don't catch&quot;, what I mean is &quot;don't catch as part of the control flow of your program&quot;.</p>

CC BY-SA 4.0
2023-07-06T15:40:53.810Z
2023-07-06T15:40:53.810Z
Silvio Mayolo
CC BY-SA 4.0
2023-07-07T00:49:21.043Z
IOException is probably 70% of the reason people hate checked exceptions
Seggan
<p>Handling unexpected and unusual conditions properly is painful in any programming language, because there are more ways for code to fail than succeed, because it is easy to forget some of the ways it can fail, and because the handling code can easily obscure the common-case code. Nevertheless, checked exceptions as in Java are one of the best mechanisms available for this purpose, when used correctly. They remind the programmer about the possible outcomes of code they are using, and they allow factoring the handling code out of the common-case code to separate handlers.</p>
<p>Using unchecked exceptions to reduce verbosity is an antipattern: in languages that don't have checked exceptions, developers tend to fail to document possible exceptions from code. For example, one study of C# code showed that the vast majority of exceptions were not documented, an omission that leads to unreliable code. When feasible, unchecked exceptions should be reserved for situations corresponding to true errors in the program: that is, programmer mistakes that the program should not normally try to recover from.</p>
<p>A type-safe alternative to checked exceptions is to wrap function results in some kind of variant (an enum or another object wrapper). This strategy also forces the programmer to reason about alternative outcomes of the computation, but in a way that is even more awkward than exceptions, since exceptional-case code must be wrapped around every function call rather than being factored into a separate handler. A less safe approach is to encode
alternative results into special values (error codes) in the function's range. Error codes are error-prone because nothing signals to the programmer to remember to watch for them.</p>
<p>In languages that support them well, monads offer another way to handle alternative execution paths. However, they do not work as well when there are multiple different kinds of exceptional behavior, requiring the unintuitive machinery of monad transformers.</p>
<p>One problem with checked exceptions is that they don't interact well with higher-order functions in Java, and in this case, a wrapper or the use of unchecked exceptions may be unavoidable. One proposal for supporting checked exceptions with higher-order functions is &quot;tunneled exceptions&quot;, described in the paper by Zhang et al.: &quot;Accepting blame for safe tunneled exceptions&quot;, in PLDI 2016. Tunneled exceptions provide a form of safe exception polymorphism for higher-order functions, avoiding the pitfalls of unchecked exceptions. That paper also explores the philosophy of exceptions in more detail.</p>

CC BY-SA 4.0
2023-07-06T14:58:53.890Z
2023-07-06T14:58:53.890Z
andru
<p>One of the big problems with Java-style checked exceptions is that while they (arguably) improve reliability, they <em>hurt</em> adaptability. If a method you call throws a new type of exception, you have to change the signature of your method as well, even if you don't actually deal with this new exception.</p>
<p>That's where <em>Anchored Exception Declarations</em> come in. They allow you to declare your checked exceptions in terms of the methods you call as opposed to directly listing every single exception explicitly:</p>
<pre class="lang-java prettyprint-override"><code>void foo(SomeClass bar, SomeOtherClass baz)
  throws like bar.quux(baz)
    propagating NotImplementedError
    blocking IOException {
  try {
    bar.quux(baz);
  }
  catch IOException {
    // Handle the exception
  }
}
</code></pre>
<p>Here, we are declaring that we can throw the same exceptions that <code>bar.quux</code> can throw when called with <code>baz</code> as an argument, except that we are going to handle <code>IOException</code> ourselves and we might also throw <code>NotImplementedError</code>.</p>
<p>So, if <code>SomeClass.quux</code> changes the exceptions it can throw, we don't have to change the definition of <code>foo</code>. Of course, <em>someone</em>, at <em>some point</em> in the call chain will have to handle these exceptions, but unlike with Java-style checked exceptions, we don't have to change <em>every single link in the call chain</em>.</p>
<p>You have to be careful to not leak private implementation details, but it has been shown that this can be done in a way where it does not violate information hiding. And if you think about it, for example the <em>Template Method</em> Design Pattern also requires knowledge about which methods are called internally … in fact, that's the whole point of that Pattern.</p>
<p>The original type checking algorithm for <em>Anchored Exceptions</em> required whole-program analysis and was therefore anti-modular, but that has since been fixed.</p>
<p>See</p>
<ul>
<li><a href="https://www2.cs.kuleuven.be/publicaties/rapporten/cw/CW407.pdf" rel="noreferrer"><em>Combining the robustness of checked exceptions with the flexibility of unchecked exceptions using anchored exception declarations</em> (Marko van Dooren, Erik Steegmans 2005)</a></li>
<li><a href="https://www.cs.kuleuven.be/publicaties/rapporten/cw/CW544.abs.html" rel="noreferrer"><em>Modular anchored exception declarations</em> (Marko van Dooren, Wouter Joosen 2009)</a></li>
<li><a href="https://sciencedirect.com/science/article/pii/S0167642313002785" rel="noreferrer"><em>Modular type checking of anchored exception declarations</em> (Marko van Dooren, Bart Jacobs, Wouter Joosen 2013)</a></li>
</ul>

CC BY-SA 4.0
2023-07-01T12:01:00.770Z
2023-07-01T12:01:00.770Z
Jörg W Mittag
CC BY-SA 4.0
2024-11-25T19:02:34.840Z
One issue with this, at least one which would make it hard for Java to adopt, is how this would be represented in the compiled `.class` file. If the exceptions from `bar.quux()` are resolved at compile-time then `Foo` needs to be recompiled whenever `Bar` changes what exceptions `quux()` can throw. On the other hand, if they are resolved at link-time then it's necessary for the linker to check that `foo()`'s signature is still compatible with any method it overrides (from a superclass or interface).
kaya3
<h3>API breakage</h3>
<p>One problem with Java exceptions, is that adding another type of exception to a function causes an API breakage if it is not a subclass to the declared exception. And of course for a function to start throwing checked exceptions is a break in itself.</p>
<p>For API evolution, this can sometimes be problematic, as it encourages methods to declare their exception type as general as possible, which is less nice for the user.</p>
<h3>Nesting</h3>
<p>Catching exceptions in Java requires nested scopes, which can be hard to read when multiple levels try-catch are used. In general, this is why you can see utility code like <code>closeSilently(file)</code>, as the explicit <code>try { if (file != null) file.close(); } catch (Exception e) { }</code> often makes the code feel harder to read.</p>
<p>C++ suffers from the same problem, so in Herb Sutter's proposal for <a href="https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0709r1.pdf" rel="nofollow noreferrer">updated C++ exception handling</a>, there is a tentative idea to try to remove the scoping:</p>
<pre><code>try return safe_divide(i, j); 
catch {
  if (err == arithmetic_errc::divide_by_zero) return 0;
  if (err == arithmetic_errc::not_integer_division) return i / j;
  if (err == arithmetic_errc::integer_divide_overflows) return INT_MIN;
}
</code></pre>
<p>While not part of the proposal, it does show a way to reduce nesting.</p>
<h3>Cumbersome to silence</h3>
<p>While</p>
<pre><code>try 
{ 
  foo(); 
} 
catch (Exception e) { }
</code></pre>
<p>And</p>
<pre><code>int z;
try 
{ 
  z = foo(); 
} 
catch (Exception e) 
{
  throw new RuntimeException(e);
}
</code></pre>
<p>Both work, they lack the ease of some syntactic sugar like:</p>
<pre><code>catch foo(); // Assume this silently catches the exception
int z = bar()!!; // Assume !! propagates the error as a runtime exception.
</code></pre>
<h3>Summary</h3>
<p>The lack of ergonomics of checked exceptions can mostly be derived from the excessive nesting and the lack of syntactic sugar when rethrowing or ignoring an exception. Because API evolution issues exceptions are often broader than useful, reducing the benefit somewhat. On the other hand, it's certainly better than unchecked exceptions in many ways, as the latter is extremely poor at documenting the actual exceptions that may occur.</p>

CC BY-SA 4.0
2023-05-18T09:09:06.873Z
2023-05-18T09:09:06.873Z
Nuoji
CC BY-SA 4.0
2023-07-01T12:54:22.713Z
Swift actually does have the things you mention, with `try?` and `try!`. The former converts `foo()` from `throws -> T` to `-> T?`, and the latter makes it just `-> T` and causes a panic on error.
Bbrk24
<p>I would argue that the main reason that Java's checked exceptions are unergonomic, is the lack of union types and type aliases. Consider this hypothetical code:</p>
<pre><code>type MyException = FooException | BarException | BazException

void someMethod() throws MyException {
    // ...
}
</code></pre>
<p>This would solve the problem of having to write <code>throws FooException, BarException, BazException</code> repeatedly across multiple methods which call each other and hence can all throw the same types of exceptions. Additionally, if another type of exception has to be added to this list after a refactoring, then it only needs to be added in one place.</p>
<hr />
<p>Another reason Java's checked exceptions are annoying is that their names are so long ─ every exception class has the word <code>Exception</code> as a suffix, 9 letters which don't convey much useful information since these class names are only written in <code>throws</code> clauses, <code>throw</code> statements and <code>catch</code> clauses, so the context already indicates that they are exception types.</p>
<p>I don't have my own recommendation for how to name exception types differently, but e.g. in Python the names are <code>IndexError</code>, <code>TypeError</code>, <code>ValueError</code> and so on instead of <code>ArrayIndexOutOfBoundsException</code> like in Java, so clearly it's possible to do better.</p>

CC BY-SA 4.0
2023-05-18T05:02:37.850Z
2023-07-01T16:09:34.083Z
2023-07-01T16:09:34.083Z
kaya3
CC BY-SA 4.0
2023-07-01T15:18:14.180Z
The verbosity of Java exception names really is ridiculous. If an index is wrong, what could be wrong with it *other than* being out of bounds? (And if such a thing were wrong, would it not be a fundamentally different problem?) If an index is out of bounds, is it really significant whether it's an array index or a string index? Does that impact on the interface to the exception object? Don't we already know that information from the context of the code? (At least the superclass `IndexOutOfBoundsException` *exists*, but still.)
Karl Knechtel
CC BY-SA 4.0
2023-07-07T00:43:57.083Z
@KarlKnechtel many bad decisions were made in Java’s early days pertaining to the standard library
Seggan
# Are checked exceptions inherently inconvenient? How to design them in a more ergonomic manner?
Posted by: username_0 on 2023-05-18
In Java, checked exceptions are well-known to be *extremely* unergonomic, to the point that many people circumvent them.

Evidently, checking exceptions, if done properly, can lead to a more robust code—which is an excellent advantage. Rust's approach, though doesn't make use of exceptions, shows this robustness improvement.

Are checked exceptions inherently painful? What designs could be used to make them more ergonomic?
**Tags:** language-design exceptions
---
## 5 answers
### Answer by username_5 on 2023-05-18. Score: 7
I would argue that the main reason that Java's checked exceptions are unergonomic, is the lack of union types and type aliases. Consider this hypothetical code:
type MyException = FooException | BarException | BazException

void someMethod() throws MyException {
    // ...
}

This would solve the problem of having to write throws FooException, BarException, BazException repeatedly across multiple methods which call each other and hence can all throw the same types of exceptions. Additionally, if another type of exception has to be added to this list after a refactoring, then it only needs to be added in one place.

Another reason Java's checked exceptions are annoying is that their names are so long ─ every exception class has the word Exception as a suffix, 9 letters which don't convey much useful information since these class names are only written in throws clauses, throw statements and catch clauses, so the context already indicates that they are exception types.
I don't have my own recommendation for how to name exception types differently, but e.g. in Python the names are IndexError, TypeError, ValueError and so on instead of ArrayIndexOutOfBoundsException like in Java, so clearly it's possible to do better.
> **Comments:**
> **username_8** (2023-07-01): The verbosity of Java exception names really is ridiculous. If an index is wrong, what could be wrong with it *other than* being out of bounds? (And if such a thing were wrong, would it not be a fundamentally different problem?) If an index is out of bounds, is it really significant whether it's an array index or a string index? Does that impact on the interface to the exception object? Don't we already know that information from the context of the code? (At least the superclass `IndexOutOfBoundsException` *exists*, but still.)  
> **username_2** (2023-07-07): @KarlKnechtel many bad decisions were made in Java’s early days pertaining to the standard library  
---
### Answer by username_4 on 2023-07-01. Score: 5
One of the big problems with Java-style checked exceptions is that while they (arguably) improve reliability, they hurt adaptability. If a method you call throws a new type of exception, you have to change the signature of your method as well, even if you don't actually deal with this new exception.
That's where Anchored Exception Declarations come in. They allow you to declare your checked exceptions in terms of the methods you call as opposed to directly listing every single exception explicitly:
void foo(SomeClass bar, SomeOtherClass baz)
  throws like bar.quux(baz)
    propagating NotImplementedError
    blocking IOException {
  try {
    bar.quux(baz);
  }
  catch IOException {
    // Handle the exception
  }
}

Here, we are declaring that we can throw the same exceptions that bar.quux can throw when called with baz as an argument, except that we are going to handle IOException ourselves and we might also throw NotImplementedError.
So, if SomeClass.quux changes the exceptions it can throw, we don't have to change the definition of foo. Of course, someone, at some point in the call chain will have to handle these exceptions, but unlike with Java-style checked exceptions, we don't have to change every single link in the call chain.
You have to be careful to not leak private implementation details, but it has been shown that this can be done in a way where it does not violate information hiding. And if you think about it, for example the Template Method Design Pattern also requires knowledge about which methods are called internally … in fact, that's the whole point of that Pattern.
The original type checking algorithm for Anchored Exceptions required whole-program analysis and was therefore anti-modular, but that has since been fixed.
See

Combining the robustness of checked exceptions with the flexibility of unchecked exceptions using anchored exception declarations (Marko van Dooren, Erik Steegmans 2005)
Modular anchored exception declarations (Marko van Dooren, Wouter Joosen 2009)
Modular type checking of anchored exception declarations (Marko van Dooren, Bart Jacobs, Wouter Joosen 2013)
> **Comments:**
> **username_5** (2024-11-25): One issue with this, at least one which would make it hard for Java to adopt, is how this would be represented in the compiled `.class` file. If the exceptions from `bar.quux()` are resolved at compile-time then `Foo` needs to be recompiled whenever `Bar` changes what exceptions `quux()` can throw. On the other hand, if they are resolved at link-time then it's necessary for the linker to check that `foo()`'s signature is still compatible with any method it overrides (from a superclass or interface).  
---
### Answer by username_6 on 2023-05-18. Score: 4
API breakage
One problem with Java exceptions, is that adding another type of exception to a function causes an API breakage if it is not a subclass to the declared exception. And of course for a function to start throwing checked exceptions is a break in itself.
For API evolution, this can sometimes be problematic, as it encourages methods to declare their exception type as general as possible, which is less nice for the user.
Nesting
Catching exceptions in Java requires nested scopes, which can be hard to read when multiple levels try-catch are used. In general, this is why you can see utility code like closeSilently(file), as the explicit try { if (file != null) file.close(); } catch (Exception e) { } often makes the code feel harder to read.
C++ suffers from the same problem, so in Herb Sutter's proposal for updated C++ exception handling, there is a tentative idea to try to remove the scoping:
try return safe_divide(i, j); 
catch {
  if (err == arithmetic_errc::divide_by_zero) return 0;
  if (err == arithmetic_errc::not_integer_division) return i / j;
  if (err == arithmetic_errc::integer_divide_overflows) return INT_MIN;
}

While not part of the proposal, it does show a way to reduce nesting.
Cumbersome to silence
While
try 
{ 
  foo(); 
} 
catch (Exception e) { }

And
int z;
try 
{ 
  z = foo(); 
} 
catch (Exception e) 
{
  throw new RuntimeException(e);
}

Both work, they lack the ease of some syntactic sugar like:
catch foo(); // Assume this silently catches the exception
int z = bar()!!; // Assume !! propagates the error as a runtime exception.

Summary
The lack of ergonomics of checked exceptions can mostly be derived from the excessive nesting and the lack of syntactic sugar when rethrowing or ignoring an exception. Because API evolution issues exceptions are often broader than useful, reducing the benefit somewhat. On the other hand, it's certainly better than unchecked exceptions in many ways, as the latter is extremely poor at documenting the actual exceptions that may occur.
> **Comments:**
> **username_7** (2023-07-01): Swift actually does have the things you mention, with `try?` and `try!`. The former converts `foo()` from `throws -> T` to `-> T?`, and the latter makes it just `-> T` and causes a panic on error.  
---
### Answer by username_1 on 2023-07-06. Score: 3
There's an excellent answer by Daniel A.A. Pelsmaeker over on our big sister site, which I'll summarize here.
In it, he classifies the "things that can go wrong in your program" into, broadly, four categories.

Fatal exceptions are stop-the-world. These are "the operating system ran out of memory" or "somebody literally pulled the plug on the computer in the middle of my program". You cannot stop these. They're not your fault. You can't handle them. You probably can't even log the exception in some cases.
Boneheaded exceptions are bugs in your program. You accidentally accessed an array index out of bounds, or you passed a negative integer to a function that only works on positive ones. That's your fault, and its your job to prevent that.
Vexing exceptions are things that happen in the normal course of programming. Trying to parse an integer can inherently fail, and that throws a NumberFormatException. A NumberFormatException doesn't mean your program is flawed. It just means the data you gave it (which may have been user input or something over the network) wasn't what you expected it to be.
Exogenous exceptions are not bugs in your code, but you have to deal with them. The difference between exogenous and vexing is that the former is exceptional, whereas the latter is something that normally happens. If you try to access a file and it doesn't exist, that's an exogenous exception. The file should exist, but something out of your control made it not so. You have to deal with the problem, though; you can't simply read from a file that doesn't exist.

In an ideal API, vexing exceptions are not exceptions. As I described above, they're not actually exceptional. They happen all the time, and it's normal. That parseInt function should probably return an Optional or (in a language like Kotlin with checked nulls) a null on failure. If you need to provide more error information, maybe a Rust-style Result is in order.
So if we're talking about an ideal world, we can assume vexing exceptions don't happen. Fatal exceptions should never be checked (and, indeed, they're unchecked in Java). They're difficult (and sometimes impossible) to catch correctly, so 99.999% of programs should just let the whole thing fail. Boneheaded exceptions also shouldn't be checked. A boneheaded exception is your fault, as the user of the API. You did something wrong and violated a precondition, and the library author was nice enough to check for that and let you know. You don't catch-and-suppress a boneheaded exception; you fix the code that caused it[1].
That leaves exogenous exceptions. These make sense as checked exceptions. Bad things happened that aren't our fault. If they occur, we're in error recovery mode, and we're definitely not in the normal "happy path" of our program anymore.
So I think, broadly, the problems with Java's checked exceptions are:

Inconsistent treatment of vexing exceptions. Parsing an integer can throw an unchecked parse error, but parsing a URI is checked.
When you have exogenous exceptions, they tend to get lumped together like the infamous IOException, which is declared in the throws clause of, let's conservatively say, 40% of the Java standard library. I might be able to handle FileNotFoundException specifically, but there's no meaningful way to respond to a throws IOException other than "catch and suppress", which is always a problem.
This was mentioned in other answers, but exceptions make higher-order functions difficult in Java. A function that takes another function (or any vaguely callable object) as argument must decide up-front whether or not the inner function can throw checked exceptions. That means a lot of functions end up in the red camp and just assume their inner functions can throw whatever.

The first bullet point can be solved by replacing vexing exceptions with something viable in the large. The last two can be solved with a better exception hierarchy and more fine-grained control over function effects. Scala is currently experimenting with a system of effects for checked exceptions.
Now you might wonder why I consider Result different from exceptions. After all, I said "vexing exceptions shouldn't exist" immediately followed by proposing a feature very similar to them. The difference is in granularity. Exceptions, at least Java-style ones, are statement-based. That is, when an exception fires off, the entire current statement stops, and at minimum, something somewhere else (even if in the same function) catches it. Vexing exceptions are inherently normal. They happen all the time, so they shouldn't stop the world and interrupt the normal flow of your program. I don't mind statement-based exceptions for exogenous cases. Those are actually exceptional, so they should stop the world and do something extravagant.
With a Rust-style Result, I choose how to handle the problem inline, as part of the normal flow of control.
let my_number = my_string.parse().unwrap_or(0);

If we want to propagate the error, we can do so. Rust has the ? operator which passes errors up to the caller.
let my_number = my_string.parse()?;

Or we can assert that the vexing exception is in fact a boneheaded one (e.g., "the caller failed a precondition, or called some unsafe function in the wrong way"), and convert the exception to an unchecked one.
let my_number = my_string.parse().expect("You did a stupid :(");

The point is, the failure state is a real, first-class object in the language, so it can be handled as part of the normal flow of control. Languages with try ... catch expressions like Kotlin move closer to this, but they're still not quite first-class values, so that's why I still advocate for Result types over actual exceptions, at least for vexing exceptions.

[1] When I say "don't catch this", I'm naturally excluding the case of a top-level try ... catch responsible for logging. A catch-all in your main is generally fine for anything non-fatal and improves your ability to iterate on code. When I say "don't catch", what I mean is "don't catch as part of the control flow of your program".
> **Comments:**
> **username_2** (2023-07-07): IOException is probably 70% of the reason people hate checked exceptions  
---
### Answer by username_3 on 2023-07-06. Score: 0
Handling unexpected and unusual conditions properly is painful in any programming language, because there are more ways for code to fail than succeed, because it is easy to forget some of the ways it can fail, and because the handling code can easily obscure the common-case code. Nevertheless, checked exceptions as in Java are one of the best mechanisms available for this purpose, when used correctly. They remind the programmer about the possible outcomes of code they are using, and they allow factoring the handling code out of the common-case code to separate handlers.

Using unchecked exceptions to reduce verbosity is an antipattern: in languages that don't have checked exceptions, developers tend to fail to document possible exceptions from code. For example, one study of C# code showed that the vast majority of exceptions were not documented, an omission that leads to unreliable code. When feasible, unchecked exceptions should be reserved for situations corresponding to true errors in the program: that is, programmer mistakes that the program should not normally try to recover from.

A type-safe alternative to checked exceptions is to wrap function results in some kind of variant (an enum or another object wrapper). This strategy also forces the programmer to reason about alternative outcomes of the computation, but in a way that is even more awkward than exceptions, since exceptional-case code must be wrapped around every function call rather than being factored into a separate handler. A less safe approach is to encode
alternative results into special values (error codes) in the function's range. Error codes are error-prone because nothing signals to the programmer to remember to watch for them.

In languages that support them well, monads offer another way to handle alternative execution paths. However, they do not work as well when there are multiple different kinds of exceptional behavior, requiring the unintuitive machinery of monad transformers.

One problem with checked exceptions is that they don't interact well with higher-order functions in Java, and in this case, a wrapper or the use of unchecked exceptions may be unavoidable. One proposal for supporting checked exceptions with higher-order functions is "tunneled exceptions", described in the paper by Zhang et al.: "Accepting blame for safe tunneled exceptions", in PLDI 2016. Tunneled exceptions provide a form of safe exception polymorphism for higher-order functions, avoiding the pitfalls of unchecked exceptions. That paper also explores the philosophy of exceptions in more detail.
---
