Java

What are Checked vs Unchecked Exceptions in Java?

In Java, exceptions are part of the Throwable hierarchy and are used to signal abnormal situations during the execution of a program. Understanding how exceptions are structured helps in designing more robust and readable code.

At the top of the hierarchy is the Throwable class, which has two direct subclasses:

  • Error: Serious problems that applications should not try to catch (e.g., OutOfMemoryError, StackOverflowError).
  • Exception: Conditions that applications might want to catch.

Checked Exceptions

Checked exceptions are exceptions that must be declared in a method or constructor’s throws clause if they can be thrown and not caught within the method. These exceptions are checked at compile time.

Examples:

  • IOException
  • SQLException
  • ParseException
public static String getContentFromResource(String resourceName) {
        try {
            InputStream stream = ResourceUtils.class.getResourceAsStream(resourceName);
            return StreamUtils.copyToString(stream, Charset.forName("UTF-8"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

If you forget to handle a checked exception, the compiler will raise an error.

When to use?
When the failure is expected (e.g., I/O issues or database access problems).

Unchecked Exceptions

Unchecked exceptions are not required to be declared or handled, and are checked at runtime. They extend RuntimeException.

Examples:

  • NullPointerException
  • IllegalArgumentException
  • IndexOutOfBoundsException
public void divide(int a, int b) {
    int result = a / b; // Can throws ArithmeticException (unchecked)
}

When to use?
When the failure indicates a programming error or incorrect use of the API.

Conclusion

Understanding the exception hierarchy helps you make more informed decisions about how your code should respond to failures. When choosing between checked and unchecked exceptions, consider the type of error and whether it can (or should) be recovered from.

Author

  • Natan Ferreira

    I am a seasoned Full Stack Software Developer with 8+ years of experience, including 6+ years specializing in Java with Spring and Quarkus. My core expertise lies in developing robust RESTful APIs integrated with Cosmos Db, MySQL, and cloud platforms like Azure and AWS. I have extensive experience designing and implementing microservices architectures, ensuring performance and reliability for high-traffic systems. In addition to backend development, I have experience with Angular to build user-friendly interfaces, leveraging my postgraduate degree in frontend web development to deliver seamless and responsive user experiences. My dedication to clean and secure code led me to present best practices to my company and clients, using tools like Sonar to ensure code quality and security. I am a critical thinker, problem solver, and team player, thriving in collaborative environments while tackling complex challenges. Beyond development, I share knowledge through my blog, NatanCode, where I write about Java, Spring, Quarkus, databases, and frontend development. My passion for learning and delivering innovative solutions drives me to excel in every project I undertake.