What you're observing is that for any endeavor, the haters often dominate the discourse, even when they don't have a majority position or a credible alternative. If they didn't hate on exceptions, they'd be whining about something else. Error handling is hard; there are many different approaches (C-style single return codes, Go-style multiple return codes, unchecked exceptions, checked exceptions, try monads, and more), and each has their pros and cons (and their supporters and haters), striking different balances of reliability and intrusiveness. Thinking there is One True Best Way here is fantasy. When people react to a particular error model, they are often reacting not to the approach itself, but examples they've encountered where it is used poorly. (Certainly Java has no shortage of examples of checked exceptions being misused.) The reality is that checked exceptions are OK, and they're what we've got, and, even if there was something that was slightly better, the disruption of trying to migrate billions of lines of code would make that impractical -- it would have to be massively better, and there's no such candidate on the horizon. Most developers intuitively realize this, and are not interested in dying on this hill, so you don't hear them in the vocal discourse, because they're too busy getting their work done. So if you want to know what to tell the newbies, tell them: don't take every rant seriously, some people just like to rant. Answer from brian_goetz on reddit.com
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-checked-vs-unchecked-exceptions
Java Checked vs Unchecked Exceptions - GeeksforGeeks
June 2, 2026 - Checked exceptions in Java are categorized based on the type of problem they represent, and they must be either handled using a try-catch block or declared using the throws keyword.
🌐
Reddit
reddit.com › r/java › if everyone hates checked exceptions, where's the alternative?
r/java on Reddit: If everyone hates checked exceptions, where's the alternative?
April 25, 2024 -

Many a blog post has been written about how Checked Exception are bad/the devil incarnate.

For all the bloviating about how bad it is, most of these articles and their comment sections lack any concrete alternatives.

For Java versions before 21, there simply doesn't seem to be a reasonable (never mind close to standardized) alternative to express that a method returns Thing or AException or BException.

For Java 21+, with sealed classes and exhaustive switches, you kind of can manually recreate a vague resemblance of e.g. Rusts Result Type. That will still lack some necessities, like enforcing checking the Error for Void methods (as in Result<Void, Err>).

So my question is:

  • If you agree that checked exceptions are bad, what alternative are you actively using right now?

  • How is your favorite library handling this? Because most still seem to use exceptions

Personally, I'm getting reaaaallly annoyed by the way people talk about exceptions online. For one, they'll point out a problem, but then fail to demonstrate a solution that wouldn't have it. For another, there's very little will, it seems, to suggest and work towards a serious alternative. How can we, as a community, warn against using a builtin feature for an important part of programming without providing alternatives? Aren't we simply screwing over newbies with these takes?

Top answer
1 of 24
22
Checked exceptions are the right tool for the job they're designed for, they simply get misused. An unexpected error but there are ways to recover -> checked exception. Example : Server unavailable. Recovery possibility: retry the operation at a later time. If there is not a way to recover it should not be a checked exception. It should be an unchecked exception. Example -> NullPointer. Something is not right somewhere, and retrying isn't going to fix it. If it is not an unexpected error (example, user enters their born on date in the future) then it should not be an exception. This should be a validation that returns some validation result, maybe just true or false, maybe a validationResult class of some sort.
2 of 24
14
What you're observing is that for any endeavor, the haters often dominate the discourse, even when they don't have a majority position or a credible alternative. If they didn't hate on exceptions, they'd be whining about something else. Error handling is hard; there are many different approaches (C-style single return codes, Go-style multiple return codes, unchecked exceptions, checked exceptions, try monads, and more), and each has their pros and cons (and their supporters and haters), striking different balances of reliability and intrusiveness. Thinking there is One True Best Way here is fantasy. When people react to a particular error model, they are often reacting not to the approach itself, but examples they've encountered where it is used poorly. (Certainly Java has no shortage of examples of checked exceptions being misused.) The reality is that checked exceptions are OK, and they're what we've got, and, even if there was something that was slightly better, the disruption of trying to migrate billions of lines of code would make that impractical -- it would have to be massively better, and there's no such candidate on the horizon. Most developers intuitively realize this, and are not interested in dying on this hill, so you don't hear them in the vocal discourse, because they're too busy getting their work done. So if you want to know what to tell the newbies, tell them: don't take every rant seriously, some people just like to rant.
Discussions

Understanding checked vs unchecked exceptions in Java - Stack Overflow
Joshua Bloch in "Effective Java" said that Use checked exceptions for recoverable conditions and runtime exceptions for programming errors (Item 58 in 2nd edition) Let's see if I understan... More on stackoverflow.com
🌐 stackoverflow.com
java - When to choose checked and unchecked exceptions - Stack Overflow
In Java (or any other language with checked exceptions), when creating your own exception class, how do you decide whether it should be checked or unchecked? My instinct is to say that a checked More on stackoverflow.com
🌐 stackoverflow.com
In Java, what are checked exceptions good for? - Software Engineering Stack Exchange
Java's checked exceptions have gotten some bad press over the years. A telling sign is that it's literally the only language in the world that has them (not even other JVM languages like Groovy and... More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
November 16, 2010
Checked exceptions: Java’s biggest mistake (2014)
I notice this especially with less experienced developers and remote calls - a lot of JS code I’ve reviewed in the past assumes the remote call will always work, yet Java code from the same developer will almost always correctly handle the situation, simply because the exception is explicitly ... More on news.ycombinator.com
🌐 news.ycombinator.com
317
114
September 23, 2020
🌐
Medium
medium.com › @ahmed.abdelfaheem › checked-and-unchecked-exceptions-in-java-6cb1c9815d32
Checked and Unchecked Exceptions in Java | by Ahmed Safwat | Medium
August 12, 2023 - The main components of the exception hierarchy are ... Checked exceptions, also known as compile-time exceptions, are exceptions that must be either caught or declared in the method signature using the throws keyword.
Top answer
1 of 16
526

Many people say that checked exceptions (i.e. these that you should explicitly catch or rethrow) should not be used at all. They were eliminated in C# for example, and most languages don't have them. So you can always throw a subclass of RuntimeException (unchecked exception).

However, I think checked exceptions are useful - they are used when you want to force the user of your API to think how to handle the exceptional situation (if it is recoverable). It's just that checked exceptions are overused in the Java platform, which makes people hate them.

Here's my extended view on the topic.

As for the particular questions:

  1. Is the NumberFormatException considered a checked exception?
    No. NumberFormatException is unchecked (= is subclass of RuntimeException). Why? I don't know. (but there should have been a method isValidInteger(..))

  2. Is RuntimeException an unchecked exception?
    Yes, exactly.

  3. What should I do here?
    It depends on where this code is and what you want to happen. If it is in the UI layer - catch it and show a warning; if it's in the service layer - don't catch it at all - let it bubble. Just don't swallow the exception. If an exception occurs, in most of the cases, you should choose one of these:

  • log it and return
  • rethrow it (declare it to be thrown by the method)
  • construct a new exception by passing the current one in constructor
  1. Now, couldn't the above code also be a checked exception? I can try to recover the situation like this? Can I?
    It could've been. But nothing stops you from catching the unchecked exception as well.

  2. Why do people add class Exception in the throws clause?
    Most often because people are lazy to consider what to catch and what to rethrow. Throwing Exception is a bad practice and should be avoided.

Alas, there is no single rule to let you determine when to catch, when to rethrow, when to use checked and when to use unchecked exceptions. I agree this causes much confusion and a lot of bad code. The general principle is stated by Bloch (you quoted a part of it). And the general principle is to rethrow an exception to the layer where you can handle it.

2 of 16
257

Whether something is a "checked exception" has nothing to do with whether you catch it or what you do in the catch block. It's a property of exception classes. Anything that is a subclass of Exception except for RuntimeException and its subclasses is a checked exception.

The Java compiler forces you to either catch checked exceptions or declare them in the method signature. It was supposed to improve program safety, but the majority opinion seems to be that it's not worth the design problems it creates.

Why do they let the exception bubble up? Isnt handle error the sooner the better? Why bubble up?

Because that's the entire point of exceptions. Without this possibility, you would not need exceptions. They enable you to handle errors at a level you choose, rather than forcing you to deal with them in low-level methods where they originally occur.

🌐
Baeldung
baeldung.com › home › java › core java › checked and unchecked exceptions in java
Checked and Unchecked Exceptions in Java | Baeldung
January 8, 2024 - In general, checked exceptions represent errors outside the control of the program. For example, the constructor of FileInputStream throws FileNotFoundException if the input file does not exist. Java verifies checked exceptions at compile-time.
🌐
Oracle
docs.oracle.com › javase › specs › jls › se7 › html › jls-11.html
11.2. Compile-Time Checking of Exceptions
March 16, 2026 - To take advantage of compile-time checking for exception handlers (§11.2), it is typical to define most new exception classes as checked exception classes, that is, as subclasses of Exception that are not subclasses of RuntimeException. ... A throw statement (§14.18) was executed. An abnormal execution condition was synchronously detected by the Java Virtual Machine, namely: evaluation of an expression violates the normal semantics of the Java programming language (§15.6), such as an integer divide by zero.
Find elsewhere
🌐
TheServerSide
theserverside.com › answer › What-are-checked-vs-unchecked-exceptions-in-Java
What are checked vs. unchecked exceptions in Java? | TheServerSide
The developers of the Java IO API anticipated that attempting to access a file that does not exist would be a relatively common occurrence, so they created a checked exception to force a developer to deal with it. In contrast to a checked exception, an unchecked exception represents an error in programming logic, not an erroneous situation that might reasonably occur during the proper use of an API.
Published   April 18, 2022
🌐
Oracle
docs.oracle.com › javase › tutorial › essential › exceptions › runtime.html
Unchecked Exceptions — The Controversy (The Java™ Tutorials > Essential Java Classes > Exceptions)
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases. Because the Java programming language does not require methods to catch or to specify unchecked exceptions (RuntimeException, Error, and their subclasses), programmers may be tempted to write code that throws only unchecked exceptions or to make all their exception subclasses inherit from RuntimeException.
🌐
Medium
medium.com › @AlexanderObregon › the-difference-between-checked-and-unchecked-exceptions-in-java-for-beginners-c3943786c40a
The Difference between Checked and Unchecked Exceptions in Java for Beginners
January 15, 2024 - The primary purpose of checked exceptions is to ensure that error handling is not ignored by the developer. By requiring these exceptions to be either caught or declared, Java ensures that the programmer is aware of potential issues and takes ...
🌐
Rollbar
rollbar.com › home › java: list of checked & unchecked exceptions
Java: List of Checked & Unchecked Exceptions | Rollbar
May 7, 2024 - List of checked & unchecked Java exceptions with links to their corresponding guide. 1.InvocationTargetException 2.NoSuchMethodException....
Top answer
1 of 16
321

Checked Exceptions are great, so long as you understand when they should be used. The Java core API fails to follow these rules for SQLException (and sometimes for IOException) which is why they are so terrible.

Checked Exceptions should be used for predictable, but unpreventable errors that are reasonable to recover from.

Unchecked Exceptions should be used for everything else.

I'll break this down for you, because most people misunderstand what this means.

  1. Predictable but unpreventable: The caller did everything within their power to validate the input parameters, but some condition outside their control has caused the operation to fail. For example, you try reading a file but someone deletes it between the time you check if it exists and the time the read operation begins. By declaring a checked exception, you are telling the caller to anticipate this failure.
  2. Reasonable to recover from: There is no point telling callers to anticipate exceptions that they cannot recover from. If a user attempts to read from an non-existing file, the caller can prompt them for a new filename. On the other hand, if the method fails due to a programming bug (invalid method arguments or buggy method implementation) there is nothing the application can do to fix the problem in mid-execution. The best it can do is log the problem and wait for the developer to fix it at a later time.

Unless the exception you are throwing meets all of the above conditions it should use an Unchecked Exception.

Reevaluate at every level: Sometimes the method catching the checked exception isn't the right place to handle the error. In that case, consider what is reasonable for your own callers. If the exception is predictable, unpreventable and reasonable for them to recover from then you should throw a checked exception yourself. If not, you should wrap the exception in an unchecked exception. If you follow this rule you will find yourself converting checked exceptions to unchecked exceptions and vice versa depending on what layer you are in.

For both checked and unchecked exceptions, use the right abstraction level. For example, a code repository with two different implementations (database and filesystem) should avoid exposing implementation-specific details by throwing SQLException or IOException. Instead, it should wrap the exception in an abstraction that spans all implementations (e.g. RepositoryException).

2 of 16
65

From A Java Learner:

When an exception occurs, you have to either catch and handle the exception, or tell compiler that you can't handle it by declaring that your method throws that exception, then the code that uses your method will have to handle that exception (even it also may choose to declare that it throws the exception if it can't handle it).

Compiler will check that we have done one of the two things (catch, or declare). So these are called Checked exceptions. But Errors, and Runtime Exceptions are not checked for by compiler (even though you can choose to catch, or declare, it is not required). So, these two are called Unchecked exceptions.

Errors are used to represent those conditions which occur outside the application, such as crash of the system. Runtime exceptions are usually occur by fault in the application logic. You can't do anything in these situations. When runtime exception occur, you have to re-write your program code. So, these are not checked by compiler. These runtime exceptions will uncover in development, and testing period. Then we have to refactor our code to remove these errors.

🌐
How to do in Java
howtodoinjava.com › home › exception handling › java checked vs unchecked exceptions
Java - Checked vs Unchecked Exceptions (with Examples)
December 20, 2022 - In Java, exceptions are broadly categorized into two sections: ... The checked exceptions are those exceptions, as the name suggests, which a method must handle in its body or throw to the caller method so the caller method can handle it.
🌐
Quora
quora.com › What-are-checked-and-unchecked-exceptions-in-Java-2
What are checked and unchecked exceptions in Java? - Quora
Answer: 1. Checked Exceptions: - Definition: These are exceptions that are checked at compile-time. The Java compiler forces you to handle these exceptions, either by using a `try-catch` block or by declaring them with the `throws` keyword. - Purpose: Checked exceptions are generally used for r...
Top answer
1 of 8
26

First of all, like any other programming paradigm you need to do it right for it to work well.

For me the advantage of checked exceptions is that the authors of the Java runtime library ALREADY have decided for me what common problems I might reasonably be expected to be able to handle at the calling point (as opposed to a top-level catch-print-die block) and consider as early as possible how to handle these problems.

I like checked exceptions because they make my code more robust by forcing me to think about error recovery as early as possible.

To be more precise, to me this makes my code more robust as it forces me to consider strange corner cases very early in the process as opposed to saying "Oops, my code does not handle if the file doesn't exist yet" based on an error in production, which you then have to rework your code to handle. Adding error handling to existing code can be a non-trivial task - and hence expensive - when reaching maintenance as opposed to just doing it right from the start.

It might be that the missing file is a fatal thing and should cause the program to crash in flames, but then you make that decision with

} catch (FileNotFoundException e) {
  throw new RuntimeException("Important file not present", e);
}

This also shows a very important side effect. If you wrap an exception, you can add an explanation which goes in the stack-trace! This is so extremely powerful because you can add information about e.g. the name of the file that was missing, or the parameters passed to this method or other diagnostic information, and that information is present right in the stack trace which frequently is the single thing you get when a program has crashed.

People may say "we can just run this in the debugger to reproduce", but I have found that very frequently production errors cannot be reproduced later, and we cannot run debuggers in production except for very nasty cases where essentially your job is at stake.

The more information in your stack trace, the better. Checked exceptions help me get that information in there, and early.


EDIT: This goes for library designers as well. One library I use on a daily basis contains many, many checked exceptions which could have been designed much better making it less tedious to use.

2 of 8
13

You've got two good answers which explain what checked exceptions have become in practice. (+1 to both.) But it would also be worthwhile to examine what they were intended for in theory, because the intention is actually worthwhile.

Checked exceptions are actually intended to make the language more type safe. Consider a simple method like integer multiplication. You might think that the result type of this method would be an integer, but, strictly speaking, the result is either an integer or an overflow exception. Considering the integer result by itself as the return type of the method does not express the full range of the function.

Seen in this light, it's not strictly true to say that checked exceptions have not found their way into other languages. They have just not taken the form that Java used. In Haskell applications, it is common to use algebraic data types to distinguish between successful and unsuccessful completion of the function. Although this is not an exception, per se, the intention is very much the same as a checked exception; it is an API designed to force the API consumer to handle both the successful in the unsuccessful case, e.g.:

data Foo a =
     Success a
   | DidNotWorkBecauseOfA
   | DidNotWorkBecauseOfB

This tells the programmer that the function has two additional possible results besides success.

🌐
Rollbar
rollbar.com › home › how to handle checked & unchecked exceptions in java
How to Handle Checked & Unchecked Exceptions in Java
July 5, 2024 - In broad terms, a checked exception (also called a logical exception) in Java is something that has gone wrong in your code and is potentially recoverable. For example, if there’s a client error when calling another API, we could retry from ...
🌐
Medium
medium.com › @firatmelih › exceptions-in-java-checked-unchecked-and-polymorphism-3712cb8ae2d4
Exceptions in Java: Checked, Unchecked and Polymorphism | by Melih Firat | Medium
November 11, 2024 - In Java, methods that can carry risks to blow up your app are declared with exceptions. These exceptions are extending either Exception class or RuntimeException then Exceptionclass. This is making the difference between checked and unchecked exceptions.
🌐
Oracle
docs.oracle.com › javase › tutorial › essential › exceptions › catchOrDeclare.html
The Catch or Specify Requirement (The Java™ Tutorials > Essential Java Classes > Exceptions)
But sometimes the user supplies the name of a nonexistent file, and the constructor throws java.io.FileNotFoundException. A well-written program will catch this exception and notify the user of the mistake, possibly prompting for a corrected file name. Checked exceptions are subject to the Catch or Specify Requirement. All exceptions are checked exceptions, except for those indicated by Error, RuntimeException, and their subclasses.
🌐
W3Schools
w3schools.com › java › java_try_catch.asp
Java Exceptions (Try...Catch)
As mentioned in the Errors chapter, different types of errors can occur while running a program - such as coding mistakes, invalid input, or unexpected situations. When an error occurs, Java will normally stop and generate an error message. The technical term for this is: Java will throw an exception (throw an error).
🌐
Hacker News
news.ycombinator.com › item
Checked exceptions: Java’s biggest mistake (2014) | Hacker News
September 23, 2020 - I notice this especially with less experienced developers and remote calls - a lot of JS code I’ve reviewed in the past assumes the remote call will always work, yet Java code from the same developer will almost always correctly handle the situation, simply because the exception is explicitly ...
🌐
Reddit
reddit.com › r/learnjava › exceptions - checked vs unchecked
r/learnjava on Reddit: Exceptions - Checked vs Unchecked
April 29, 2020 -

A checked exception required a throw clause on the method header but not for the unchecked exception?

What is the difference between the two? How do I know if it is unchecked vs checked?