Académique Documents
Professionnel Documents
Culture Documents
1.
Exceptions are sent directly to a caller of the failed method.
2.
3.
Can exceptions can be overlooked?
4.
Someone withdraws too much money from a bank account. Is the bank account an
IllegalStateException?
5.
To signal an exceptional condition, use a try statement to try an an exception object.
6.
Does a method immediately terminates when you throw an exception?
7.
You place the statement that can cause the exception inside a ?? block and handler inside
a ?? clause .
8.
How should you modify the deposit method to ensure that the balance is never negative?
9.
Suppose you construct a new Bank account with zero balance and then call withdraw(10).
What is the balance afterwards?
10.
Describe checked exceptions and give examples of checked exceptions.
11.
Do the same for unchecked exceptions.
12.
Why are there two kinds of exceptions?
13.
What happens when an exception has no handler?
14.
What is a try block and catch clause?
15.
What is throw early, catch late mean?
16.
What is wrong with this exception handling?
Try
{ FileReader reader= new FileReader(filename);
// Compiler complained about FileNotFoundException.
}
catch ( Exception e) {} // so there!
Equivalent question : Why should you not shut the compiler up by squelching
exceptions?
17.
Should the new exception type such as InsufficientFundException be unchecked or
checked exception? What class should it extend?
18.
19.
Why don’t you need to declare that your method might throw a NullPointerException?
20.
What happens if an exception does not have a matching clause?
21.
Is the type of the exception object always the same as the type declared in the catch
clause that matches it?
22.
What is the purpose of the finally clause? Give an example of how it can be used.
23.
Which exception can the next and NextInt methods of the Scanner class throw? Are they
checked exceptions or unchecked exceptions?
24.
How can a program be improved to give a more accurate error report?
6.
Describe in detail: what is a finally block and show the basic framework for a try
statement with a finally block
7.
Why must the next method be called in the catch block to dispose users invalid input?
What happens if you omit the statement that calls next?
Answers:
1.
FALSE- to the exception handler
2.
yes
3.
false
4.
No. The parameter value is illegal. throw IllegalArgumentException.
5.
False, use a throw to throw an exception object.
6.
yes
7.
Try, catch.
8.
Throw an exception if the amount deposited is less than zero.
9.
The balance is still zero since the last statement of the withdraw method was never
executed.
10.
When you call a method that throws a checked exception , the compiler checks you
don’t ignore it . You must specify what to do with it if it is ever thrown. Examples:
IOException , Exception subclasses.
11.
The compiler does not require you to check exceptions. Example:
IllegalArgumentException , NumberFormatException, NullPointerException , and
all subclasses of RunTimeException .
12.
A checked exception describes a problem that is likely to occur at times ,
no matter how careful you are. An unchecked exception, are your fault.
13.
A error message is printed and program terminates.
Therefore, you should provide exception handlers for all exceptions that your
program might throw.
14.
Each try block contains one or more statements that may cause an exception. Each
catch clause contains the handler for an exception type .
15.
It s better to throw an exception rather than try to come up with an imperfect fix (
returning default value, or doing nothing).
16.
Installing an incompetent handler hides the error condition that could be serious.
Example:
17.
18.
You throw an exception to notify some other part of the program that an exceptional
condition has occurred.You catch an exception to handle an exceptional situation
that has occurred elsewhere in the program.
19.
A NullPointerException is an unchecked exception. The programmer is to
be blamed when such an exception occurs because it is the responsibility of the
programmer to test all references for null before using them instead of installing a
handler for that exception.
20.
If an exception has no matching catch clause in the same method, the search for a
matching catch clause continues in the calling method. If the entire program has no
matching catch clause, then the program terminates.
21.
No, the type of the thrown exception may be a subtype of the type of the caught
exception. That commonly happens, for example, when throwing an EOFException
and catching an IOException.
22.
They are all unchecked exceptions. The designers of the Scanner class made this
choice to make the class easy to use for beginning programmers.
23.
7.
The next method must be called in the catch block to dispose of the
user’s invalid input because the nextInt method leaves the input value
in the Scanner’s input stream if an InputMismatchException is
thrown. If you omit the statement that calls next, the while loop keeps
reading it, throws an exception, and displays an error message in an infinite
loop.