-
Notifications
You must be signed in to change notification settings - Fork 1
Module 9 Mastery Check
wolvesled edited this page Sep 5, 2013
·
1 revision
- What class is at the top of the exception hierarchy? Answer: Throwable
- Briefly explain how to use try and catch. Answer: use try { } block to enclose the code that may generate exception, and followed with one or more catch (exception e) { } block for each specific exception that to handle.
- What is wrong with this fragment?
// ... vals[18] = 10; catch (ArrayIndexOutObBoundsException exc) { // handle error }Answer: testing code must be enclosed in a try block. - What happens if an exception is not caught? Answer: it will eventually caught by JVM and terminate the program.
- What is wrong with this fragment?
class A extends Exception { ... class B extends A { ... // ... try { // ... } catch (A exc) { ... } catch (B exc) { ... }Answer: making the catch of superclass exception before catch of subclass exception will make the subclass exception catch block unreachable, which generates an error. - Can an exception caught by an inner catch rethrow that exception to an outer catch? Answer: Yes, but only to the enclosing outer catch.
- The finally block is the last bit of code executed before your program ends. True of False? Answer: False, it is the code executed after a try/catch block either exception is caught or not.
- What type of exceptions must be explicitly declared in a throws clause of a method? Answer: those checked exceptions.
- What is wrong with this fragment?
class MyClass { // ... } // ... throw new MyClass();Answer: MyClass is not inherit from Throwable, cannot be throw like an exception. - In question of the Mastery Check in Module 6, you create a Stack class. Add custom exceptions to your class that report stack full and stack empty conditions.
- What are the three ways that an exception can be generated? Answer: exception can be generated by an error in JVM, or from the program, also can be manually generated.
- What are the two direct subclasses of Throwable? Answer: Exception and Error