Chapter 6 Excaptions Flashcards
Tell the subclass of the following exceptions ? Also if they are ok for programmer to catch and is the program required to handle or declare:
- Runtime exception
- Checked exception
- Error
- Runtime exception - Subclass of RuntimeException - it’s ok for programmer to catch - the program is no required to handle or declare it
- Checked exception - Subclass of Exception but not subclass of RuntimeException - it’s ok for programmer to catch - the program is required to handle or declare it
- Error - Subclass of Error - it’s not ok for programmer to catch - the program is not required to handle or declare it
What is ArithmeticException ?
It is a Runtime Exceptions.
Trying to divide an int by zero gives an undefi ned result. When this occurs, the JVM will throw an ArithmeticException:
What is ArrayIndexOutOfBoundsException ?
It is a Runtime Exceptions.
You know by now that array indexes start with 0 and go up to 1 less than the length of the array—which means this code will throw an ArrayIndexOutOfBoundsException:
int[] countsOfMoose = new int[3];
System.out.println(countsOfMoose[-1]);
What is ClassCastException ?
It is Runtime Exception.
Java tries to protect you from impossible casts. This code doesn’t compile because Integer is not a subclass of String: String type = "moose"; Integer number = (Integer) type; // DOES NOT COMPILE
What is IllegalArgumentException ?
It is Runtime Exception.
This exception indicates that a method is called with incorrect input arguments.
What is NullPointerException ?
It is Runtime Exception.
Instance variables and methods must be called on a non-null reference. If the reference is null, the JVM will throw a NullPointerException.
What is NumberFormatException ?
It is a Runtime Exception.
Java provides methods to convert strings to numbers. When these are passed an invalid value, they throw a NumberFormatException. The idea is similar to IllegalArgumentException.
What is FileNotFoundException ?
It is a Checked Exception.
Thrown programmatically when code tries to reference a fi le that does not exist
What is IOException ?
It is a Checked Exception.
Thrown programmatically when there’s a problem reading or writing a file.
What is ExceptionInInitializerError ?
It is an Error ?
Thrown by the JVM when a static initializer throws an exception and doesn’t handle it.
What is StackOverflowError ?
It is an Error .
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).
What is NoClassDefFoundError ?
It is an Error.
Thrown by the JVM when a class that the code uses is available at compile time but not runtime.
Explain differences and similarities between:
java. lang.Error
java. lang.Exception
java. lang.RuntimeException
Subclasses of java.lang.Error are exceptions that a programmer should not attempt to handle. Subclasses of java.lang.RuntimeException are runtime (unchecked) exceptions. Subclasses of java.lang.Exception, but not java.lang.RuntimeException are checked exceptions. Java requires checked exceptions to be handled or declared.
Which exceptions are thrown by the programmer and which are thrown by the JVM:
- ArithmeticException
- ArrayIndexOutOfBoundsException
- ClassCastException
- IllegalArgumentException
- NullPointerException
- NumberFormatException
- IOException
- FileNotFoundException
- ExceptionInInitializerError
- StackOverflowError
- NoClassDefFoundError
IllegalArgumentException and NumberFormatException are typically thrown by the programmer, whereas the others are typically thrown by the JVM.
Also, java.io.IOException is thrown by many methods in the java.io package, but it is always thrown programmatically.
What is System.exit ?
There is one exception to “the finally block always runs after the catch block” rule: Java defines a method that you call as System.exit(0);. The integer parameter is the error code that gets returned. System.exit tells Java, “Stop. End the program right now. Do not pass go. Do not collect $200.” When System.exit is called in the try or catch block, finally does not run.