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.