What is an exception in Java? What are checked and unchecked exceptions?

An exception is an event which stops the normal flow of the program. Java gives an object of the exception, and you can catch it with try and catch.

TypeMust handle?Example
CheckedYes, the compiler forces itIOException, SQLException
Unchecked (RuntimeException)NoNullPointerException, ArithmeticException
ErrorNo, do not catchOutOfMemoryError
public class Main {
    public static void main(String[] args) {
        try {
            System.out.println(10 / 0);
        } catch (ArithmeticException e) {
            System.out.println("Caught: " + e.getMessage());
        } finally {
            System.out.println("finally runs always");
        }
    }
}
Output

Caught: / by zero
finally runs always

See Java Exceptions.


Learn more about the similar topics:
Tutorials
Java Exceptions: try, catch, finally and throw
Exercises & Assignments
Create a custom exception for an invalid age
Interview Questions & Answers
What is an exception in Java? What are checked and unchecked exceptions?
What is the difference between final, finally and finalize in Java?
What is the difference between throw and throws in Java?
What are the best practices for exception handling in Java?