Java Exceptions: Checked vs. Unchecked

Close-up view of blue program code

Java Exceptions: Checked vs. Unchecked

In the realm of Java programming, exceptions are events that disrupt the normal flow of program execution. Two primary types of exceptions exist in Java: checked and unchecked. Understanding the distinction between these two is crucial for robust and efficient Java programming.

AspectChecked CasesUnchecked Cases
DefinitionAny case which must be handled or caught while writing the program is called checked.Any case which is not mandatory to handle or catch while writing or executing the program is called unchecked.
Example1. FileNotFoundException: FileReader file = new FileReader(“file.txt”); 2. IOException: InputStream input = new FileInputStream(“input.txt”);1. NullPointerException: Employee e1 = null; System.out.println(e.name); 2. ArrayIndexOutOfBounds: int a[] = new int[10]; System.out.println(a[20]);
HierarchyThey are all subclasses of the Exception class.They are all subclasses of the RuntimeException class.
IgnoranceMust be formally acknowledged in the program one way or another.Can be ignored completely in the code if desired.
CompilationIf your code fails to either handle a checked case or declare that it is thrown, your code won’t compile.If your code fails to either handle an Unchecked case or declare that it is thrown, your code will compile.
Handling VerificationChecked cases in Java are those whose handling is verified during Compile time.Unchecked cases in Java are those whose handling is not verified during Compile time.
JVMJVM enforces the developer to handle or catch checked cases.JVM does not enforce the developer to handle or catch unchecked cases.
PropagationBy default, Checked cases are not forwarded in the calling chain (propagated).By default Unchecked cases are forwarded in the calling chain (propagated).
Caught/UncaughtThey are also called as caught cases.They are also called as uncaught cases.
Throws ClauseThe throws clause on a method header must be included for checked cases that are not caught and handled in the method.The throws clause on a method header is not mandatory to be included for unchecked cases that are not caught and handled in the method.
ScenarioChecked cases represent scenarios with a higher failure rate.Unchecked cases are mostly programming errors.
UseExample scenarios: – File reading error when the file is deleted before the operation begins, anticipating this failure for the user to try another filename. – Business logic validation error, such as dividing a number by zero.Example scenarios: – Upload failure due to network loss, anticipating this failure for the user to attempt another upload or resume from where it stopped.- User input validation error when a non-digit is entered instead of a digit.

Conclusion

Finally, understanding and managing exceptions, both checked and unchecked, is vital to becoming adept at Java programming. These skill sets enable developers to predict, capture, and handle potential errors during the code execution, ensuring the creation of reliable and error-resistant applications. Remember, effective error handling improves not only the robustness but also the readability of your code, ultimately making you a better Java programmer. If you’re looking to enhance your Java programming skills, it’s advisable to delve deeper into the concept of blocks in Java.