Java Exceptions  «Prev  Next»

Throwing Exceptions in Java - Quiz

Each question is worth one point. Select the best answer for each question.
 
1. When would you throw an exception? Please select the best answer.
  A. When you are checking the format of user input
  B. When you are anticipating a runtime error
  C. When you want to inform the calling routine of a problem

2. What is wrong with the following snippet (assuming error is a Boolean)?
if (error) {
   throw NumberFormatException;
}
Please select the best answer.
  A. We never made a new instance of the exception class we want to throw.
  B. There is no such class as a NumberFormatException.
  C. The throw keyword cannot be used in this way.


3. What is wrong with the following?
class Trouble {
   void rightHere() {
      throw new Exception();
   }
}
Please select the best answer.
  A. You cannot throw an instance of the Exception class.
  B. The throw keyword is not used correctly
  C. The method rightHere() is not defined correctly


Your score is 0.0
C
A
C