Chapter 6 : Exceptions
A runtime exception is defined as the RuntimeException class and its subclasses. Runtime exceptions tend to be unexpected but not necessarily fatal. also known as unchecked exceptions.
A checked exception includes Exception and all subclasses that do not extend RuntimeException. Checked exceptions tend to be more anticipated
Classes listed in the throws part of a method declaration must extend
java.lang.Throwable. This includes Error, Exception, and RuntimeException.
handle or declare rule
For checked exceptions, Java requires the code to either handle them or declare them in the method signature.
- Checked exceptions are required to be handled or declared.
- Runtime exceptions are allowed to be handled or declared.
- Errors are allowed to be handled or declared, but this is bad practice.
- ArithmeticException Thrown by the JVM when code attempts to divide by zero
- ArrayIndexOutOfBoundsException Thrown by the JVM when code uses an illegal index to access an array
- ClassCastException Thrown by the JVM when an attempt is made to cast an object to a subclass of which it is not an instance
- NullPointerException Thrown by the JVM when there is a null reference where an object is required
- IllegalArgumentException Thrown by the programmer to indicate that a method has been passed an illegal or inappropriate argument
- NumberFormatException Thrown by the programmer when an attempt is made to convert a string to a numeric type but the string doesn’t have an appropriate format. NumberFormatException is a subclass of IllegalArgumentException
- IOException Thrown programmatically when there’s a problem reading or writing a file
- FileNotFoundException Thrown programmatically when code tries to reference a file that does not exist. FileNotFoundException is a subclass of IOException.
- ExceptionInInitializerError Thrown by the JVM when a static initializer throws an exception and doesn’t handle it
- StackOverflowError Thrown by the JVM when a method calls itself too many times (this is called infinite recursion because the method typically calls itself without end)
- NoClassDefFoundError Thrown by the JVM when a class that the code uses is available at compile time but not runtime
A try statement must have a catch or a finally block.
finally is typically used to close resources such as files or databases
A rule exists for the order of the catch blocks. Java looks at them in the order they appear. If it is impossible for one of the catch blocks to be executed, a compiler error about unreachable code occurs. This happens when a superclass is caught before a subclass.
no superclass exception type appears in an earlier catch block than its subclass, otherwise, compile error.
If both catch and finally throw an exception, the one from finally gets thrown.
When a method overrides a method in a superclass or interface, it is not allowed to add new checked exceptions. It is allowed to declare fewer exceptions or declare a subclass of a declared exception.
it’s okay to declare new runtime exceptions in a subclass method because the declaration is redundant. Methods are free to throw any runtime exceptions they want without mentioning them in the method declaration.