6 Exceptions Flashcards
What is another name for runtime exception?
unchecked exceptions
What is the exception where finally does not run?
System.exit(0);
What do you need to know about exceptions for the exam?
The OCA exam can define basic (made up) exceptions to show you the hierarchy. You only need to do two things with this information. 1) you must be able to recognize if the exception is a checked or an unchecked exception. 2) you need to determine if any of the exceptions are subclasses of the others. class AnimalsOutForAWalk extends RuntimeException { } class ExhibitClosed extends RuntimeException { } class ExhibitClosedForLunch extends ExhibitClosed { }
In this example, there are three custom exceptions. All are unchecked exceptions because they directly or indirectly extend RuntimeException.
What types of exceptions do you need to know for the exam?
You need to recognize three types of exceptions for the OCA exam:
1- runtime exceptions,
2 checked exceptions,
3- and errors.
Name 6 Runtime Exceptions
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 exception
to a subclass of which it is not an instance
IllegalArgumentException Thrown by the programmer to indicate that a method has
been passed an illegal or inappropriate argument
NullPointerException Thrown by the JVM when there is a null reference where an
object is required
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
What is an error?
Errors extend the Error class. They are thrown by the JVM and should not be handled or declared. Errors are rare,
Name 3 common errors
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
T or F
Methods are free to throw any runtime exceptions they want without mentioning them in the method declaration.
True - The reason that it’s okay to declare new runtime exceptions in a subclass method is that the declaration is redundant. Methods are free to throw any runtime exceptions they want without mentioning them in the method declaration.
What is the difference between throws and throw?
Throws is in the declaration
Throw is when it is thrown
void fall() throws Exception { throw new Exception(); }
What do block and clause mean? as in try block and catch clause
Same thing, just the block of code between the braces for try and catch
Are braces required around the code after try and catch?
Yes, It will not compile without braces
Does the order of try, catch and finally blocks matter?
yes. It must be in the order try, catch and finally. Finally must be last.
Does finally have to be last in the try, catch, finally block?
yes. It will not compile if there is a finally that isn’t last.
Is a catch statement required if there is a try statement?
Catch is not required if finally is present. It just must have one or the other and can have both.
T or F
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.