Option 2 (Exceptions): If you forget the check, Java automatically throws ArithmeticException. But it’s an unchecked exception, so the compiler doesn’t force you to handle it.
This raises a question: Why does Java sometimes force you to handle errors, and other times let them slip by until runtime? That’s exactly what we’ll explore now.
Errors like OutOfMemoryError indicate system-level failures. You almost never handle them directly—if your program runs out of memory, there’s often little you can do.
Answers: For IOException, Java does (A)—it’s a checked exception. For ArithmeticException, Java does (B)—it’s unchecked, so you’re not forced to handle it at compile time. That’s the heart of checked vs. unchecked.
Some failures, like a missing file or a network issue, are beyond our control. Java has a rule: if a method might fail in this way, it must warn whoever calls it. This is why we use throws IOException—it’s Java’s way of saying:
public class ExceptionDemoSkeleton {
// Checked exception example
public static void readData() throws ??? {
// What checked exception might we throw here?
// e.g., throw new ???("Unable to read data...");
}
// Unchecked exception example
public static int safeDivide(int x, int y) {
// What if y == 0?
// Return x / y or do a manual check?
return ???;
}
}
Think: Which standard checked exception could you throw? (Hint: IOException is a typical example for "something went wrong reading data.") And what should you do for safeDivide if y == 0?
Notice that the compiler demands we handle or declare IOException but doesn’t require us to handle ArithmeticException. That’s the essence of checked vs. unchecked exceptions in action.
public static void setAge(int age) {
if (age < 0) {
// IllegalArgumentException is unchecked, but communicates exactly what happened
throw new IllegalArgumentException("Age cannot be negative.");
}
// ... proceed with setting age
}
Now it’s clear to the caller that the argument was illegal. Specific exception types convey more meaning than a generic Exception.
That’s the essence of working with Java’s standard exceptions. Next up, we’ll continue exploring how to handle exceptions effectively (via try/catch and throws) so your programs never fail silently—and your code remains clear and predictable.
Error represents critical system-level failures (e.g., OutOfMemoryError) that are usually not handled, while Exception represents problems your code can potentially catch and recover from.
Exactly. Error is generally fatal for the JVM, whereas Exception is recoverable.
They have no difference at runtime; Error is just a deprecated name for Exception.
No. Error is a distinct hierarchy for system-level issues.
Any Error is a compile-time issue, while Exception is always a runtime-only class.
No. Both occur at runtime; Error is not a compile-time phenomenon.
Error is for user input mistakes, while Exception is strictly for developer errors in code logic.
No. User input mistakes typically lead to Exceptions; Error signals bigger issues (like memory exhaustion).
Checked exceptions are only caused by arithmetic operations (like dividing by zero), while unchecked exceptions come from external issues (like missing files).
No. It’s actually the opposite: dividing by zero triggers an ArithmeticException, which is unchecked.
Java forces you to handle or declare unchecked exceptions, but not checked ones.
No. It’s reversed. Checked exceptions must be handled or declared; unchecked do not require that.
Checked exceptions (e.g., IOException) must be caught or declared using throws, while unchecked exceptions (e.g., ArithmeticException) don’t require explicit handling.
Correct. That’s the fundamental difference in how Java treats checked vs. unchecked.
They are identical in behavior, but checked exceptions only happen at compile-time and unchecked happen at runtime.
No. All exceptions occur at runtime; "checked" means the compiler enforces handling.
Generic Exception causes Java to skip the catch block entirely, making the program terminate immediately.
No. Java doesn’t skip catch blocks if the type matches. The problem is lack of clarity.
It’s too vague. A more specific exception (e.g. IllegalArgumentException) conveys clearer intent, helping both the caller and any catch blocks respond appropriately.
Precisely. A specific exception type clarifies the exact nature of the error.
Throwing Exception is not valid Java code and won’t compile unless you add a final finally block.
No. It is valid code. The problem is that using a generic type is poor design, not compilation failure.
It’s not discouraged; in modern Java, Exception is always replaced automatically by RuntimeException at runtime.
No automatic replacement occurs. The developer must choose an appropriate type.