OCAJP Chapter 7 - Exception Handling
What is the Exception Hierarchy?
...
With what can you follow a try block?
A try block may be followed by either a catch or a finally block or both.
Another example of a Runtime Exception is ___________________Exception, which is thrown when a piece of code tries to access an array of list elements at a nonexistent position.
ArrayIndexOutOfBoundsException
___________________ is a runtime exception that's thrown when a piece of code tries to access an array position out of its bounds‚ when an array is accessed either at a position less than 0 or at a position greater than or equal to its length.
ArrayIndexOutOfBoundsException
The order in which the catch blocks are placed matters. If the caught exceptions have an inheritance relationship, the _____ class exceptions can’t be caught before the _____ class exceptions. An attempt to do this will result in compilation failure.
Base, Derived
What is the purpose of a catch block?
Catch blocks are used to include alternative code to execute when an exceptional condition arises.
FileNotFoundException is what kind of exception? This exception is thrown if the file that the code is trying to access can’t be found.
Checked Exception
Which exception is an unacceptable condition foreseen by the author of a method, but outside the immediate control of the code.
Checked Exception
What do you do to code that may throw an exception?
Enclose it with a try block.
An error is a subclass of the class java.lang._______
Error
Subclasses of the class java.lang._______ are categorized as errors
Error
The class java.lang.RuntimeException is a subclass of the class java.lang.__________.
Exception
Exception handlers separate the concerns of...
Exception handlers separate the concerns of defining the regular program logic from exception-handling code.
All checked exceptions are a subclass of the java.lang.__________ class, and the class java.lang.RuntimeException is a subclass of the java.lang.__________ class
Exception, Exception
Subclasses of the class java.lang.__________ are categorized as checked exceptions if they are not subclasses of class java.lang.__________.
Exception, Runtime
True or False: An OutOfMemoryError will be caught by an exception handler if one is specified
False
True or False: A finally block will not execute if a try or catch block defines a return statement.
False. A finally block will execute even if a try or catch block defines a return statement.
True or False: A runtime exception must be added to the method signature when a method may throw it.
False. A runtime exception isn't a part of the method signature, even if a method may throw it.
True or False: A try, catch, or finally block cannot define another try-catch-finally block.
False. A try, catch, or finally block can define another try-catch-finally block.
True or False: An error may or may not need to be a part of a method signature.
False. An error need not be a part of a method signature.
True or False: In typical programming conditions, the ArrayIndexOutOfBoundsException should be thrown programmatically.
False. In typical programming conditions, the ArrayIndexOutOfBoundsException shouldn't be thrown programmatically.
True of False: The catch blocks must be followed by a finally block.
False. The catch blocks must be followed by zero or one finally block.
True or False: The finally block can appear before a catch block.
False. The finally block can not appear before a catch block.
True or False: the finally block only executes when an exception is thrown in the try block.
False. The finally block executes regardless of whether the code in the try block throws an exception.
Doing what, enables you to define the main logic of your code together.
Handling exceptions separately
ClassCastException is thrown when an object fails an ______ test with the class type it is being cast to.
IS-A
________________________ is thrown to specify that a method has been passed illegal or inappropriate arguments.
IllegalArgumentException
____________________ is a runtime exception that's thrown when a piece of code tries to access a list position that's out of its bounds‚ when the position is either less than 0 or greater than or equal to the lists size.
IndexOutOfBoundsException
The class ArrayIndexOutOfBoundsException extends the class java.lang.___________________, which extends the class java.lang.__________________
IndexOutOfBoundsException, RuntimeException
Proper handling of exceptions is important in Java. It enables a programmer to define what?
It enables a programmer to define alternative code to execute in case an exceptional condition is met.
If a method calls another method that may throw a checked exception what must the method do?
It must either be enclosed in a try-catch block or the method should declare this exception to be thrown in its method signature
An error is a serious exception, thrown by the __________ as a result of an error in the environment state, which processes your code.
JVM
The ____ keeps an account of all the methods that were called when it hits the code that throws an exception. To find an appropriate _________ __________, it looks through all these method calls.
JVM, exception handler
The JVM may send the stack trace of an unhanded exception to the ______ ______.
Java console
A ____________________ Error is an error thrown by the JVM when it is unable to locate the .class file it is supposed to run.
NoClassDefFound
Which of the try, catch, finally blocks can exist independently?
None
The class java.lang.Throwable inherits the class java.lang._______.
Object
NullPointerException is what type of exception that occurs when a piece of code tries to execute some code on a variable that hasn’t been assigned an object and points to null.
Runtime Exception
Which exceptions represent programming errors? These occur from inappro-priate use of another piece of code.
Runtime Exceptions
Subclasses of the class java.lang._________ are categorized as runtime exceptions.
RuntimeException
_________________ is another error, thrown by the JVM when the size of the memory required by the stack of the Java program is greater than what the JRE has offered for the Java application. This error usually occurs as a result of infinite or highly nested loops.
StackOverflowError
What will happen if the base class exception is caught before the a derived class exception in a try/catch block?
The code will fail to compile.
If both catch and finally blocks define return statements, the calling method will receive the value from which block.
The finally block.
How many levels of nested exceptions can be created?
There is no limit to the levels of nested exceptions that can be created.
The three categories of exceptions share what kind of relationship?
These three categories share the 'IS-A' relationships
Why does the JVM take responsibility for throwing the ArrayIndexOutOfBoundsException?
This exception isn't known until runtime and depends on the array or list position that's being accessed by a piece of code. Most often, a variable is used to specify this array or list position, and its value may not be known until runtime.
How do you handle errors?
Though you can handle the errors syntactically, there is little that you can do when these errors occur.
The class java.lang.Error is also a subclass of the class java.lang._________.
Throwable
The class java.lang.Exception is a subclass of the class java.lang.__________.
Throwable
True or False: A runtime exception may not necessarily be caught by a try-catch block.
True
True or False: A try block can be followed by one or more catch blocks.
True
True or False: A try block can't define multiple finally blocks.
True
True or False: For the OCA Java SE 7 Programmer I exam, you need to be aware of commonly occurring exception categories and classes, when are they thrown, whether you should handle them, and how to handle them.
True
True or False: The order in which the catch blocks are placed matters.
True
True or False: You can create nested exception handlers.
True
True or False: You can re-throw an error that you catch in an exception handler.
True
If a catch block returns this data type, a finally block can’t modify the value being returned by it.
What is a primitive data type.
You must do one of which two things with a checked exception?
You must either handle it or throw it.
A finally block alone wouldn't suffice with a try block if code in the try block throws a checked exception. In this case, what would you need to do for the code to compile?
You need to catch the checked exception or define in the method signature that the exception is thrown, or your code won't compile.
Separate exception handlers enable you to define ______________ _________ to execute separately from your main code flow when an exceptional condition is met.
alternative code
The stack trace of an exception is available within what?.
an exception handler
Trying to call a method recursively without defining an exit condition is considered what type of condition.
an exceptional condition
Exceptions are divided into what three categories?
checked exceptions, runtime , and errors.
Trying to execute a method that throws a checked exception without handling the thrown exception causes what type of error?
compilation error
Trying to access an array element at a position that is greater than or equal to the arrays length is considered what type of condition.
exceptional
Exception handlers refer to the blocks of code that execute when an _________ ___________ arises during code execution.
exceptional condition
The stack trace of an exception enables you to get a list of all the _____________ that executed before the exception was reported.
exceptions
Without the use of separate exception handlers, the main logic of your code would be lost doing what?
in combating the exceptional conditions
You can use the operator _________ to verify whether an object can be cast to another class before casting it.
instanceof
An exception is an object of what class?
java.lang.Throwable
When a piece of code hits an obstacle in the form of an exceptional condition, it creates an object of subclass ___________, initializes it with the necessary information , and hands it over to the _____.
java.lang.Throwable, state, JVM
Exceptions also help pinpoint the __________ ______, together with the method in which it is defined, by providing a stack trace of the exception or error.
offending code
Even though IllegalArgumentException is a _________ exception, programmers usually use this exception to validate the arguments that are passed to a method, and the exception constructor is passed a descriptive message specifying the exception details.
runtime
ClassCastException is a ___________ exception. java.lang.ClassCastException extends java.lang.______________.
runtime, RuntimeException
IllegalArgumentException is a _________ exception. java.lang.IllegalArgumentException extends java.lang._______________.
runtime, RuntimeException
IllegalStateException is a _________ exception. java.lang.IllegalStateException extends java.lang._____________.
runtime, RuntimeException
If a catch block returns an object, a finally block can modify the ______ being returned by it.
value