Term
What are the two options available to a method that runs into an exception? |
|
Definition
1. Handle it in the method, or 2. pass the handling of the exception to the calling method. |
|
|
Term
What is the superclass of all exceptions? |
|
Definition
|
|
Term
What is a child class of Throwable that means something so horrible happened that your program should not even attempt to handle it? |
|
Definition
|
|
Term
_____ exceptions are also called _____ exceptions that are unexpected, but not necessarily fatal. |
|
Definition
|
|
Term
_____ are exceptions that are more expected, like a process or method that is known to have some elements that may not exist or are known to fail from time to time, and is made up of the _____ and all subclasses, except for _____ exceptions and its subclasses. |
|
Definition
Checked, Exception, Runtime |
|
|
Term
Describe the handle or declare rule for exceptions, and which exception classes are affected by it. |
|
Definition
The handle or declare rule requires that for all Exceptions and subclasses (except for Runtime and its subclasses) that are checked exceptions, they must either be handled in the method, or must be declared in the method signature and thrown back to the caller. |
|
|
Term
True or false - the throws clause in the signature of a method means that the method must throw one of the exceptions listed. |
|
Definition
False - the method may or may not throw an exception depending upon what happens in the method. |
|
|
Term
True or False - NullPointerException is direct child class of Exception. |
|
Definition
False, NullPointerException is a Runtime Exception, or unchecked exception, this exception can happen at any time in the code and code would be littered with checks for this if it were designated a checked exception - its best to fix the code so these do not occur. |
|
|
Term
When a method has exception handling code with the _____ key word, it is basically saying send this exception to the caller and have them handle it, throwing it over the wall so to speak. |
|
Definition
Throws in the declaration |
|
|
Term
Subclasses of _____ Exception are _____ exceptions and are ok for the program to catch, but the program is not required to handle or declare. |
|
Definition
|
|
Term
Subclasses of _____ exception, but not _____ exception are called _____ exceptions, and are ok for the program to catch, and Java requires that they are handled or declared. |
|
Definition
Exception, Runtime, checked |
|
|
Term
_____ are subclasses of Throwable, and are not advised by Java to have the coder even attempt to handle this situation because the exception is so horrible, and it is not ok for the program to catch, nor is it ok for the program to handle or declare it. |
|
Definition
|
|
Term
Java uses the ____ statement block to separate out the logic that might cause an exception from the logic that handles the exception. |
|
Definition
|
|
Term
True or False - curly braces are not required for try or catch blocks. |
|
Definition
False, they are blocks, so the curly braces are required. |
|
|
Term
True or false - a try statement must have a matching catch and/or finally block associated with it. It must have one or the other. |
|
Definition
|
|
Term
True or false - in a try block, catch is not required if there is a finally block. |
|
Definition
|
|
Term
True or false - there is no particular order for the try, catch, finally blocks. |
|
Definition
False, try must be first, and it must be followed by catch, and then followed by finally. However, finally cannot be followed by catch, but finally can follow try (meaning there is no catch clause).
try{
}catch(Exception e){
}finally{
}
OK
Try{
}catch(Exception e){
}
OK
try{
}finally{
}
OK
try{
}finally{
}catch(Exception e){
}
NOT OK
|
|
|
Term
What is the one exception to the rule that the finally block always executes in a try/catch/finally block? |
|
Definition
If System.exit(0); is called in the try or catch blocks, they finally block is not called. |
|
|
Term
True or false - when catching multiple exceptions, if they are inherited, the parent class must be the last one, and the ones inherited from it must be in order from lowest (farthest from parent) class to highest (closest to parent). |
|
Definition
|
|
Term
True or false - if exception classes are unrelated or do not share a direct parent, it does not matter regarding the order in which they are listed in the catch clauses. |
|
Definition
|
|
Term
True or false - on the certification exam for OCA, if you see example code related to reading or writing files, you can be sure this only a syntax or exception type question. |
|
Definition
|
|
Term
What is the result of the following try block?
26: try {
27: throw new RuntimeException();
28: } catch (RuntimeException e) {
29: throw new RuntimeException();
30: } finally {
31: throw new Exception();
32: }
|
|
Definition
Before line 29 throws the new Runtime Exception, it will go into the finally block. Once that happens, the new Exception is actually the one that will be thrown. |
|
|
Term
What is returned from the following code?
30: public String exceptions() {
31: String result = "";
32: String v = null;
33: try {
34: try {
35: result + = "before ";
36: v.length();
37: result + = "after ";
38: } catch (NullPointerException e) {
39: result + = "catch";
40: throw new RuntimeException();
41: } finally {
42: result + = "finally ";
43: throw new Exception();
44: }
45: } catch (Exception e) {
46: result + = "done";
47: }
48: return result;
49: } |
|
Definition
before catch finally done |
|
|
Term
What are common Runtime exceptions thrown by the JVM? |
|
Definition
ArithmeticException - when code attempts to divide by zero
ArrayIndexOutOfBounds - when code tries to read a value from an array that does not have that index
ClassCastException - when code is trying to cast a class to a subclass, the type of which it is not an instanceof
IllegalArgumentException - thrown by the programmer to indicate that an illegal parameter has been passed into a method
IllegalStateException - signals when a method has been invoked at an illegal or inappropriate time, usually thrown by the application.
NullPointerException - when code is trying to invoke methods on a null object
NumberFormatException - when code attempts to convert a string to a numeric type, but the string does not have the appropriate value or format
SecurityException - thrown by security manager upon security violation. Typically thrown by programmer.
|
|
|
Term
NumberFormatException is a subclass of _____. |
|
Definition
|
|
Term
True or false - A FileNotFoundException is thrown when you try to close a connection. |
|
Definition
False - this checked exception occurs when code tries to reference a file that does not exist. |
|
|
Term
True or false - IOException occurs when there is a problem reading or writing a file. |
|
Definition
True - and this is a checked exception. |
|
|
Term
Name 3 common Errors, and what they are.
|
|
Definition
ExceptionInInitializerError - thrown when an exception occurs in a static initializer
StackOverflowError - thrown when a method calls itself too many times, infinite recursion
NoClassDefFoundError - thrown by JVM when a class that was available at compile time is no longer available at runtime.
|
|
|
Term
What is different about infinite recursion and an infinite loop? Which is worse? |
|
Definition
Infinite recursion occurs when a method calls itself over and over and cannot stop - Java runs out of room on the stack until it overflows and generates this error. An infinite loop is code in a looping mechanism that cannot escape.
An infinite loop is worse because it will not generate an error, and will just lock up your CPU. |
|
|
Term
Is the following code legal?
class CantHopException extends Exception{
}
class Bunny{
public static void hop(){
}
}
class BabyBunny extends Bunny{
public static void hop() throws Exception{
}
} |
|
Definition
No - when you override a method, you are not allowed to add new Exceptions, only the same or those that are subclasses of the original exception. |
|
|
Term
Why is the following code legal?
class Hopper {
public void hop(){
}
}
class Bunny extend Hopper{
public void hop() throws IllegalArgumentException{
}
}
|
|
Definition
Because overridden methods can throw any Runtime, unchecked exceptions that are not required to be handled or declared. |
|
|
Term
True or false - classes listed in the throws part of a method declaration must extend Throwable. This includes, Error, Exception, and Runtime. |
|
Definition
|
|
Term
True or false - Exception classes can be used as return types in a method declaration. |
|
Definition
|
|
Term
True or false - you can declare any subclass of Object in the the throws clause of a method declaration. |
|
Definition
False, only subclasses of Throwable. |
|
|
Term
True or false - Runtime Exception and its subclasses, or all unchecked exceptions, can be used at any time, even in overridden method declarations where the original method has no throws clause. |
|
Definition
|
|
Term
True or false - if a method has a throws clause (declares), and has a try catch (handles) which potentially can throw CHECKED exception(s), the calling method must handle or declare to make sure the CHECKED exception is dealt with, even in the main method. |
|
Definition
|
|
Term
True or false - in a method that does not declare a checked exception, and does not have code to handle one, it can still throw a checked exception, and the method will compile. |
|
Definition
False - it cannot throw a checked exception, because one is not declared, however it can throw an unchecked exception, RuntimeException or subclass. |
|
|
Term
Name two exceptions that are always thrown programmatically. |
|
Definition
Java.IO.IOException
NumberFormatException |
|
|
Term
Watch for methods that declare an exception in the throws, and the catch has that exception and a parent, make sure catching what's thrown as the declaration. |
|
Definition
|
|
Term
True or false - IOException is a part of java.lang pkg, and there is no need for a separate import. |
|
Definition
False - you need to import java.io.IOException in order to use this class. |
|
|
Term
True or false - a label can be assigned to a code block (not a declaration). If that code block contains a try/catch block, and if a break statement is encountered in the try block, the finally block will still be run before it breaks out of the code block. |
|
Definition
|
|
Term
True or false - if you have a hierarchy of exceptions, and it throws the most specific one, you can still catch all of the others in the hierarchy and it will compile and run.
class E1 extends Exception {}
class E2 extends E1 {}
class declaration...
try{
throw new E2();
}catch(E2 e){
}catch(E1 e){
}catch(Exception e){
}
|
|
Definition
|
|
Term
True or false - if a client can be reasonably expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from an exception make it an unchecked exception. Also unchecked are geared toward internal sources of exceptions (null reference, array beyond its length), while checked exceptions are more for external sources of exceptions (like trying to access a file). |
|
Definition
|
|