An exception in Java is an object that represents an abnormal event during program execution and interrupts the normal flow of control. The core split in the language is checked vs unchecked exceptions: checked exceptions must be caught or declared with throws, while unchecked exceptions (subclasses of RuntimeException and Error) are not enforced by the compiler.
Understanding this split decides how you design method signatures, how callers recover, and when a failure is a bug rather than a condition to handle.
When a method hits a problem it cannot handle, it creates an exception object and throws it. The JVM then unwinds the call stack, looking for a matching catch block. If none is found, the thread terminates and prints the stack trace.
The finally block runs whether or not an exception was thrown, which makes it suitable for cleanup. For resources such as files and sockets, prefer try-with-resources instead.
All throwable types descend from java.lang.Throwable. It has two direct branches, Error and Exception, and RuntimeException is a subclass of Exception.
The rule is simple: Throwable and its subclasses are checked, except RuntimeException, Error and their subclasses, which are unchecked.
A checked exception is verified at compile time. If code can throw one, the method must either catch it or list it in a throws clause. Otherwise the code does not compile.
Checked exceptions model conditions that a well-written program can reasonably expect and recover from, such as a missing file, a network failure or a database error. Common examples are IOException, SQLException, FileNotFoundException and InterruptedException.
Unchecked exceptions extend RuntimeException. The compiler does not force you to catch or declare them. They usually signal programming mistakes: a null reference, a bad index, an illegal argument or an invalid state.
The right fix for most unchecked exceptions is to correct the code or validate input earlier, not to wrap everything in try/catch.
| Aspect | Checked | Unchecked |
|---|---|---|
| Base class | Exception (not RuntimeException) | RuntimeException or Error |
| Compiler enforcement | Must catch or declare | None |
| Typical cause | External conditions (I/O, network, database) | Programming bugs |
| Caller can recover? | Often yes | Usually no, fix the code |
| Examples | IOException, SQLException | NullPointerException, IllegalStateException |
Choose the base class by what you want callers to do. Extend Exception to force handling, or RuntimeException when the failure is a bug or when forcing every caller to handle it would only add noise.
When you rethrow a lower-level failure as a different exception, pass the original as the cause, for example throw new ServiceException("load failed", e), so the stack trace keeps the full history.
Exception or Throwable.catch block empty. Log, rethrow or recover.AutoCloseable.Catching Exception broadly hides bugs and handles unrelated failures the same way. It is acceptable at a top-level boundary, such as a request handler that logs and returns an error response, but not deep inside business logic.
Yes. Unchecked exceptions can be caught like any other. The compiler just does not require it. Catch them only when you can do something useful, such as returning a default or reporting a clear message.
They add throws clauses up the call chain and work awkwardly with lambdas and streams, because functional interfaces do not declare them. Many modern libraries therefore prefer unchecked exceptions, while the JDK still uses checked ones for I/O.
The thread ends and the JVM prints the stack trace to standard error. In the main thread the program exits with a non-zero status once no other non-daemon threads remain.
An exception is an object thrown when something goes wrong. Checked exceptions are enforced by the compiler and describe recoverable external conditions, while unchecked exceptions (RuntimeException and Error subclasses) usually point to bugs. Pick the type by what the caller can do, catch narrowly, and always keep the original cause when wrapping.