Understanding Java Exception Handling: Checked vs Unchecked Exceptions
This article explains Java exception handling by distinguishing checked and unchecked exceptions, demonstrates compilation outcomes for different exception types, and provides practical guidelines, code examples, and best practices for declaring, catching, and throwing exceptions in Java programs.
Code
public class ExceptionTypeTest {
public void doSomething() throws ArithmeticException {
System.out.println();
}
public static void main() {
ExceptionTypeTest ett = new ExceptionTypeTest();
ett.doSomething();
}
}Question 1: Can the program compile? Explain the reason.
Answer: Yes, it can compile. The method doSomething declares ArithmeticException , which is a subclass of RuntimeException (an unchecked exception). The Java compiler does not require unchecked exceptions to be caught or declared further.
Question 2: If ArithmeticException is changed to IOException , can the program compile? Explain the reason.
Answer: No, it cannot compile. IOException extends Exception and is a checked exception, so the compiler forces the code to either catch the exception or declare it with throws in the method signature.
Summary: Java exceptions are divided into two main categories: checked exceptions and unchecked exceptions (runtime exceptions). Unchecked exceptions do not need to be explicitly handled or declared, while checked exceptions must be either caught with a try/catch block or declared to be thrown.
Exception Handling (Exception)
1. Exceptions
An exception is an abnormal event that occurs during program execution, interrupting the normal flow of instructions. Exceptions happen at runtime; compile‑time problems are syntax errors.
2. Exception Handling Mechanism
1) When an exception occurs, the JVM automatically creates an exception object and throws it to the runtime system.
2) The runtime system searches for an appropriate handler near the code that caused the exception.
3. Types of Exceptions
java.lang.Throwable
Error: serious JVM problems that cannot be recovered from; developers do not handle them.
Exception: normal problems that can be handled to allow the program to continue.
4. Common Exceptions
Unchecked exceptions: NullPointerException , ClassCastException , ArrayIndexOutOfBoundsException , ArithmeticException (e.g., divide‑by‑zero).
Checked exceptions: Exception , FileNotFoundException , IOException , SQLException .
5. Two Ways to Handle Exceptions
1) Explicitly use try/catch (generally discouraged for unchecked exceptions).
try {
// code that may throw an exception
} catch (ExceptionType e) {
// handle the exception
e.printStackTrace();
} finally {
// code that always runs, e.g., resource cleanup
}2) Declare the exception with throws and let the caller handle it (recommended for checked exceptions).
6. Manually Throwing an Exception
Use the throw keyword inside a method to abort execution when business logic is violated.
Note: If the thrown exception is checked, the method must either catch it or declare it with throws . If it is unchecked, handling is optional.
7. Custom Exceptions
Define a new exception by extending Exception for a checked exception, or extend RuntimeException for an unchecked one, to represent domain‑specific error conditions.
8. Exception Overview
Two handling strategies: catch and process locally, or propagate upward using throws .
9. Practical Tips
Observe the exception name and line number; catch and handle known exceptions, propagate unknown ones; keep try blocks small; avoid deep nesting; always log the stack trace in catch blocks.
Using the throws Keyword
The throws clause declares that a method may throw one or more exceptions, separated by commas.
If the declared exception is unchecked, callers may choose to handle it or ignore it.
If the declared exception is checked, callers must handle it.
When manually throwing a checked exception, it must be either caught or declared.
Explicit try/catch handling (generally not ideal for unchecked exceptions).
Declare with throws and let the caller handle it (recommended for checked exceptions).
Always catch and handle exceptions you know how to deal with, and propagate those you do not.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.