Questions
Who should be responsible for catching and handling exceptions: the computer or the computer program?
1. The Classification of Exceptions
Before discussing who is responsible for catching and handling exceptions, let’s first classify exceptions because different exceptions require different handling.
There are two kinds of exceptions in Java: Unchecked exceptions and checked exceptions. As shown in the above picture, the classes for unchecked exceptions are shown in blue, and the classes for checked exceptions are shown in red.
1.1. Unchecked exceptions
Unchecked exceptions: “Any exception that derives from the class Error or the class RuntimeException” (Horstmann, 2019, p. 511); “unchecked” means that these exceptions are not checked at compile-time but at runtime.
Error: “The Error hierarchy describes internal errors and resource exhaustion situations inside the Java runtime system” (Horstmann, 2019, p. 510); this kind of exception is checked at runtime.
RuntimeException: RuntimeException is usually caused by programmers and should be fixed in programs, for example, ArrayIndexOutOfBoundsException and NullPointerException are the most common RuntimeExceptions that can be fixed by modifying the code; this kind of exception is checked at runtime.
1.2. Checked exceptions
Checked exceptions: Any exception that derives from the class Exception except for RuntimeException(Eck, 2019), for instance, FileNotFoundException and IOException are the most common checked exceptions; “checked” means that these exceptions are checked at compile-time; unlike unchecked exceptions, checked exceptions require mandatory handling. In this case, we can add the throws keyword to the method header to throw exceptions for the caller to handle. Or use try…catch… blocks to handle such exceptions directly.
2. The roles responsible for catching and handling different exceptions
There are two roles responsible for exception handling. On the one hand, the programmer can explicitly handle some exceptions by modifying the program. In other words, the computer program is responsible for handling these types of exceptions. On the other hand, there are exceptions that are beyond the developer’s control and cannot be handled explicitly, so the JVM handles them. That is, the computer is responsible for handling these exceptions.
2.1. Error
The computer is responsible for catching and handling exceptions derived from the Error class. This is because exceptions derived from the Error class are usually fatal and beyond one’s control, so programmers should avoid catching or throwing such exceptions in their programs. Instead, let the JVM handle these exceptions.
For example, if there is some illegal data in the compiled Java class, a ClassFormatError will occur (Eck, 2019). However, we cannot catch and handle such errors. Instead, the computer reports an error and handles it when it occurs.
2.2 RuntimeException
The computer program is responsible for catching and handling exceptions derived from the class RuntimeException. This is because RuntimeExceptions are usually caused by programmers and can be fixed in code.
For example, ArrayIndexOutOfBoundsException is a RuntimeException. We can avoid this exception by checking the index before using it. Therefore, the programmer can avoid this exception by modifying the code. In other words, the computer program is responsible for catching and handling RuntimeException.
2.3 Checked exceptions
The computer program is responsible for catching and handling checked exceptions. This is because checked exceptions need to be handled forcibly. Otherwise, the compiler will issue an error message.
For example, FileNotFoundException is a checked exception. So, we need to either use a try…catch… block to handle this exception or throw this exception in the method header.
Codes:
1 | import java.io.FileNotFoundException; |
1 | import java.io.FileNotFoundException; |
3. In conclusion
The computer (JVM) is responsible for catching and handling exceptions derived from the class Error; the computer program is responsible for catching and handling exceptions derived from the class Exception, including the exceptions derived from the class RuntimeException and all the checked exceptions.
Word count: 669
References
1.Eck, D. J. (2019). Introduction to Programming Using Java.
http://math.hws.edu/javanotes
2.Horstmann, C. S. (2019). Core Java Volume I–Fundamentals (11th ed.). Pearson.