Session 11 |
Exception class hierarchy, checked and unchecked exceptions
All exception types are subclasses of the built-in class Throwable
. Thus, Throwable
is at the top of the exception class hierarchy. Immediately below Throwable
are two subclasses: Exception
and Error
.
The Exception
class is used for exceptional conditions that user programs should catch. This is also the class that we will subclass to create your own custom exception types. There is an important subclass of Exception
, called RuntimeException
. Exceptions of this type are automatically defined for the programs that we write and include things such as division by zero and invalid array indexing.
The Error
class, which defines exceptions that are not expected to be caught under normal circumstances by our program. Exceptions of type Error
are used by the Java run-time system to indicate errors having to do with the run-time environment, itself. Stack overflow is an example of such an error. We will not be dealing with exceptions of type Error
, because these are typically created in response to catastrophic failures that cannot usually be handled by your program.
Both the Exception
class and the Error
class have a large number of subclasses. Check the documentation https://docs.oracle.com/javase/8/docs/api/java/lang/Exception.html
.
RuntimeException
, Error
, and their subclasses are known as unchecked exceptions because the compiler does not check if a method handles or throws these exceptions. Therefore, these exceptions do not have to be included in any method’s throws
list. The unchecked exceptions defined in java.lang
are listed below.
All other exceptions are known as checked exceptions, meaning that the compiler forces the programmer to check and deal with them:
- in a
try/catch
block or - declare it in the method header so that the caller of the method is informed of the exception. To declare an exception in a method, use the
throws
keyword in the method header. Athrows
clause lists the types of exceptions that a method might throw. If they are not, a compile-time error will result.
A list of checked exceptions is shown below:
For example, if we use BufferedReader
class in our program to read data from a file and the file specified in the constructor does not exist, a FileNotFoundException
occurs, and the compiler prompts the programmer to handle the exception.
class FileReader2 { public static void main(String[] args) throws IOException { BufferedReader myReader = new BufferedReader(new InputStreamReader(new FileInputStream(args[0]), "UTF-8")); myReadLines(myReader); myReader.close(); } // remove the "throws IOException" and check the output of the program private static void myReadLines(BufferedReader myReader) throws IOException { while (true) { String line = myReader.readLine(); // can throw IOException if (line == null) break; // end of the file System.out.println(line); } } }
NB! IDEs usually generate the following code for the checked exceptions; however, such code without solutions to the exceptions is not correct - rather use the throws
with the type of exceptions.
try { someMethodThatThrowsException(); } catch (Exception e) { e.printStackTrace(); // TODO autogenerated catch -- DO NOT DO LIKE THIS }
Session 11 |