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.

Answer from Bozho on Stack Overflow
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ tutorial โ€บ essential โ€บ exceptions โ€บ runtime.html
Unchecked Exceptions ย— The Controversy (The Javaโ„ข Tutorials > Essential Java Classes > Exceptions)
Here's the bottom line guideline: If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-checked-vs-unchecked-exceptions
Java Checked vs Unchecked Exceptions - GeeksforGeeks
October 2, 2025 - In Java, there are two types of ... to handle them explicitly. Unchecked Exception: These exceptions are checked at runtime and do not require explicit handling at compile time....
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ checked and unchecked exceptions in java
Checked and Unchecked Exceptions in Java | Baeldung
January 8, 2024 - The Oracle Java Documentation provides ... to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception.โ€ ยท For example, before we open ...
๐ŸŒ
Rollbar
rollbar.com โ€บ home โ€บ how to handle checked & unchecked exceptions in java
How to Handle Checked & Unchecked Exceptions in Java | Rollbar
July 5, 2024 - Unchecked exceptions are like running out of gas or getting a flat tire. These are things that could happen due to your own oversight. You're not legally required to constantly check your fuel gauge or tire pressure, but if these problems occur, ...
๐ŸŒ
TheServerSide
theserverside.com โ€บ answer โ€บ What-are-checked-vs-unchecked-exceptions-in-Java
What are checked vs. unchecked exceptions in Java? | TheServerSide
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.
Top answer
1 of 16
521

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.

๐ŸŒ
Reddit
reddit.com โ€บ r/java โ€บ unchecked java: say goodbye to checked exceptions forever
r/java on Reddit: Unchecked Java: Say Goodbye to Checked Exceptions Forever
July 13, 2023 - This is what most libraries are doing for us now. They still throw documented exceptions, it's just that they're unchecked. There's a reason no other languages, including all the java spinoffs, have checked exceptions.
๐ŸŒ
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 - Because unchecked exceptions typically indicate issues that should be fixed during development, they can often be avoided through proper coding practices, such as input validation and defensive programming. When working with exceptions in Java, itโ€™s important to follow some best practices to ensure the maintainability and reliability of your code:
Find elsewhere
๐ŸŒ
Coding Shuttle
codingshuttle.com โ€บ java-programming-handbook โ€บ checked-and-unchecked-exceptions
Checked vs Unchecked Exceptions in Java: Ultimate Guide | Coding Shuttle
July 24, 2025 - Unchecked Exceptions โ€“ Exceptions that occur at runtime and do not require mandatory handling. ... Checked exceptions are exceptions that must be handled using a try-catch block or declared using throws in the method signature.
๐ŸŒ
Dev.java
dev.java โ€บ learn โ€บ exceptions โ€บ unchecked-exception-controversy
Unchecked Exceptions โ€” The Controversy - Dev.java
If a client cannot do anything to recover from the exception, make it an unchecked exception. Now that you know what exceptions are and how to use them, it's time to learn the advantages of using exceptions in your programs.
๐ŸŒ
Medium
medium.com โ€บ @devcorner โ€บ unchecked-exceptions-in-java-to-handle-or-not-to-handle-bd617efad76c
Unchecked Exceptions in Java: To Handle or Not to Handle? ๐Ÿค” | by Dev Corner | Medium
November 29, 2024 - Unchecked exceptions in Java are a subclass of RuntimeException. Unlike checked exceptions, they do not need to be declared in the throws clause or wrapped in try-catch blocks.
๐ŸŒ
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 - Unchecked exceptions are the exceptions that the compiler does not require to catch or declare. They generally occur due to programming errors and are derived from java.lang.RuntimeException.
๐ŸŒ
BYJUS
byjus.com โ€บ gate โ€บ difference-between-checked-and-unchecked-exceptions-in-java
Differences between Checked and Unchecked Exceptions ...
September 28, 2022 - There are two types of exceptions: ... A checked exception is an exception that should be reported in the method in which it is thrown. ... An exception that occurs at the runtime or at the time of execution is known as an unchecked exception.
๐ŸŒ
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 - If we are creating any custom exception, then the rule is if a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception. ...
๐ŸŒ
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?

๐ŸŒ
Jenkov
jenkov.com โ€บ tutorials โ€บ java-exception-handling โ€บ checked-or-unchecked-exceptions.html
Checked or Unchecked Exceptions?
Checked exceptions must be explicitly caught or propagated as described in Basic try-catch-finally Exception Handling. Unchecked exceptions do not have this requirement. They don't have to be caught or declared thrown. Checked exceptions in Java extend the java.lang.Exception class.
๐ŸŒ
Scaler
scaler.com โ€บ topics โ€บ unchecked-exception-in-java
What Is an Unchecked Exception in Java? - Scaler Topics
October 7, 2022 - In Java, exceptions that are under Error and , Runtime exception classes are unchecked exceptions.
๐ŸŒ
Java Mex
javamex.com โ€บ tutorials โ€บ exceptions โ€บ exceptions_unchecked.shtml
Unchecked exceptions in Java
Overview of unchecked exceptions in Java, which don't need to be explicitly caught and which methods can always throw without declaring so.
๐ŸŒ
GitHub
github.com โ€บ qoomon โ€บ unchecked-exceptions-java
GitHub - qoomon/unchecked-exceptions-java: Throw any Java Exception anywhere without the need of catching them nor wrapping them into RuntimeException
unchecked(LambdaFunction) returns result or rethrows exception from lambda function as unchecked exception, without wrapping exception. return unchecked(() -> methodThrowingCheckedException()) import static me.qoomon.UncheckedExceptions.*; public ...
Starred by 8 users
Forked by 4 users
Languages ย  Java
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.