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
🌐
GeeksforGeeks
geeksforgeeks.org › java › exceptions-in-java
Java Exception Handling - GeeksforGeeks
Exception Handling in Java is a mechanism used to handle both compile-time (checked) and runtime (unchecked) exceptions, allowing a program to continue execution smoothly even in the presence of errors.
Published   April 1, 2026
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Exception.html
Exception (Java Platform SE 8 )
April 21, 2026 - Constructs a new exception with the specified detail message, cause, suppression enabled or disabled, and writable stack trace enabled or disabled. ... cause - the cause. (A null value is permitted, and indicates that the cause is nonexistent or unknown.) enableSuppression - whether or not suppression is enabled or disabled · writableStackTrace - whether or not the stack trace should be writable ... Java™ Platform Standard Ed.
🌐
W3Schools
w3schools.com › java › java_try_catch.asp
Java Exceptions (Try...Catch)
As mentioned in the Errors chapter, ... 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)....
🌐
Medium
medium.com › @ShantKhayalian › mastering-java-exception-handling-a-comprehensive-guide-for-developers-c31cef921d75
Mastering Java Exception Handling: A Comprehensive Guide for Developers | by Shant Khayalian | Medium
June 17, 2024 - Unchecked exceptions are subclasses of RuntimeException. Errors: These are serious issues that are not typically caught by the application code. Examples include OutOfMemoryError and StackOverflowError. Errors are subclasses of Error and usually indicate problems that the application cannot recover from. ... Java provides a structured way to handle exceptions using five key keywords: try, catch, finally, throw, and throws.
🌐
YouTube
youtube.com › coding with john
Exception Handling in Java Tutorial - YouTube
Complete Java course: https://codingwithjohn.thinkific.com/courses/java-for-beginnersEverything you need to know about how to handle Exceptions in Java with ...
Published   December 31, 2020
Views   394K
🌐
JetBrains
blog.jetbrains.com › home › intellij idea › easy hacks: how to throw java exceptions
Easy Hacks: How to Throw Java Exceptions - The JetBrains Blog
March 12, 2024 - Exceptions in Java are used to indicate that an event occurred during the execution of a program and disrupted the normal flow of instructions. When an exception occurs, the Java runtime automatically stops the execution of the current method.
Find elsewhere
🌐
Reddit
reddit.com › r/java › my personal definitive guide to (java) exceptions
r/java on Reddit: My personal definitive guide to (java) Exceptions
March 1, 2021 - Exceptions in Java are either checked (which means your code is polluted by rethrowing them or catching them and wrapping them in unchecked exceptions) or unchecked (which is also bad because the caller isn't made explicitly aware that this exception can happen so you are bound to forget to catch it).
🌐
Tutorialspoint
tutorialspoint.com › java › java_exceptions.htm
Java - Exceptions
The exception class is a subclass of the Throwable class. Other than the exception class there is another subclass called Error which is derived from the Throwable class. Errors are abnormal conditions that happen in case of severe failures, ...
🌐
Raygun
raygun.com › blog › java-exceptions-terminology
Java exceptions: Common terminology with examples · Raygun Blog
October 25, 2022 - Java exceptions are events that disrupt the normal execution of the program. The main goal of exceptions is to separate error-handling from regular code. Exceptions might stem from a wide range of problems such as missing resources, coding errors, ...
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.

🌐
Medium
medium.com › @janithsjay › what-is-an-exception-and-how-exceptions-work-in-java-fa6d4f6897b4
What is an Exception and how exceptions work in Java ? | by Janith Jayasuriya | Medium
August 5, 2022 - When an unexpected event or error occurs within a method, the method creates an object and hands it off to the runtime system. The object, called an exception object, contains information about the error, including its type and the state of ...
🌐
Medium
medium.com › @prayukti › exceptions-in-java-a-guide-to-exception-handling-76c1268c81d5
Exceptions in Java — A Guide to Exception Handling | by Prayukti Jain | Medium
June 1, 2024 - Exceptions can either be built-in exceptions or user defined exceptions. An exception would be considered as a valid in Java only and only if it extends or inherits the Exception class.
🌐
Medium
medium.com › @quipoin04 › types-of-exceptions-in-java-a-complete-beginner-friendly-guide-09b6d35e93c0
Types of Exceptions in Java — A Complete Beginner-Friendly Guide | by Quipoin | Medium
December 9, 2025 - Let’s understand each in detail with examples. Checked exceptions are checked at compile time. This means the compiler forces you to handle them using try-catch or throws. They usually occur due to external factors (files, networks, database access, etc.) ... import java.io.*; public class CheckedExample { public static void main(String[] args) throws IOException { FileReader file = new FileReader("data.txt"); BufferedReader reader = new BufferedReader(file); System.out.println(reader.readLine()); reader.close(); } }
🌐
Oracle
docs.oracle.com › javase › specs › jls › se11 › html › jls-11.html
Chapter 11. Exceptions
March 16, 2026 - Every exception is represented by an instance of the class Throwable or one of its subclasses (§11.1). Such an object can be used to carry information from the point at which an exception occurs to the handler that catches it. Handlers are established by catch clauses of try statements (§14.20). During the process of throwing an exception, the Java Virtual Machine abruptly completes, one by one, any expressions, statements, method and constructor invocations, initializers, and field initialization expressions that have begun but not completed execution in the current thread.
Top answer
1 of 2
2

You could define a functional interface to represent actions performed on any Element that might throw a FrameworkException. Then, you just need to implement a method that accepts both the action and the current item. This method both encapsulates the try-catch logic and applies the action to the item.

@FunctionalInterface
public interface ElementAction {
  void execute(Element element) throws FrameworkException;
}
public void tryAction(ElementAction action, Element element) {
  try {
    action.execute(element);
  } catch (FrameworkException e) {
    // do whatever you usually do for exceptions
  }
}

This way you can have your processItems be cleaner like this:

public void processItems(List<Element> items) {
  for (Element item : items) {
    tryAction(element -> framework.doSomething(item), item);

    // . . .

    tryAction(element -> framework.doDifferentThing(item), item);

    // . . .
    }

    // cont.
}

edit(just read the comments on the main post):

You can extend the tryAction method by passing in a BiConsumer that takes in an Element and a FrameworkException for custom loggin.

@FunctionalInterface
public interface ExceptionLogger extends BiConsumer<Element, FrameworkException> {}

You would then add it to the tryAction method:

public void tryAction(ElementAction action, Element element, ExceptionLogger logger) {
  try {
    action.execute(element);
  } catch (FrameworkException e) {
    logger.accept(element, e);
  }
}

Then in your processItems you would add your way of logging the exception:

public void processItems(List<Element> items) {
  for (Element item : items) {
    tryAction((
      element -> framework.doSomething(item), 
      item,
      (element, e) -> System.err.println("this error " + e.getMessage())
    );

    // . . .

    tryAction(
      element -> framework.doDifferentThing(item),
      item,
      (element, e) -> System.err.println("that error " + e.getMessage())
    );

    // . . .
    }

    // cont.
}

(this is assuming you are using Java 8+, which you really should be these days)

2 of 2
1

It sounds like you want the loop to continue whenever a framework exception occurs:

for (Element item : items) {
    try {
        framework.doSomething(item);

        // ...

        framework.doDifferentThing(item);
    } catch (FrameworkException | SomeOtherFrameworkException e) {
        logger.log(Level.INFO, "Corrupted element " + item, e);
    }
}

This allows your logic to appear continuous, without being visually broken up by multiple catch blocks.

You should not be seeking to use some magical annotation to hide try/catch blocks from other developers. Spreading out your logic into other places without any reference to them would just make future maintenance dramatically more difficult.

In this case, though, I don’t think there would be any way to do it, regardless. You have to catch an exception if you want to recover from it and continue the loop.

🌐
GeeksforGeeks
geeksforgeeks.org › java › built-exceptions-java-examples
Built-in Exceptions in Java with examples - GeeksforGeeks
July 3, 2024 - 2. ArrayIndexOutOfBounds Exception: It is thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array. ... // Java program to demonstrate // ArrayIndexOutOfBoundException class ArrayIndexOutOfBound_Demo { public static void main(String[] args) { try { int[] a = new int[5]; a[5] = 9; // accessing 6th element in an array of // size 5 } catch (ArrayIndexOutOfBoundsException e) { System.out.println("Array Index is Out Of Bounds"); } } }
🌐
Medium
medium.com › @brandon93.w › java-exceptions-a-comprehensive-guide-e27636b6300e
Java Exceptions: A Comprehensive Guide | by Brandon Wohlwend | Medium
August 8, 2023 - Here are a few common scenarios where exceptions come into play: User Input Errors, e.g. when you should input your age and you write “banana”. External Factors, e.g. when you want your program to read in a file from your harddrive, but the file doesn’t exists. Resource Contraints, e.g. if your program tries to connect to a database, but the database server is down. Java provides a hierarchy of classes that play specific roles in managing exceptions:
🌐
Medium
medium.com › softaai-blogs › java-exceptions-explained-a-beginners-guide-to-handling-errors-188caf6133b8
Java Exceptions Explained: A Beginner’s Guide to Handling Errors | by amol pawar | softAai Blogs | Medium
July 17, 2025 - In Java, an exception is an event that disrupts the normal flow of a program. It occurs when something unexpected happens, like dividing by zero or accessing an invalid array index.
🌐
Medium
thelogiclooms.medium.com › understanding-java-exceptions-a-beginner-friendly-guide-589bff2dbdf3
Understanding Java Exceptions: A Beginner-Friendly Guide | by Vipul Kumar | Medium
December 16, 2025 - These are all subclasses of Throwable except RuntimeException and Error. The compiler forces you to handle them in one of two ways: ... They usually represent recoverable problems. These problems are caused by conditions outside the control of the program. ... Since these issues can occur even if the code is correct, Java ensures that developers explicitly deal with them.
🌐
Oracle
docs.oracle.com › javase › tutorial › essential › exceptions › catch.html
The catch Blocks (The Java™ Tutorials > Essential Java Classes > Exceptions)
Each catch block is an exception handler that handles the type of exception indicated by its argument. The argument type, ExceptionType, declares the type of exception that the handler can handle and must be the name of a class that inherits from the Throwable class.