User Defined Exceptions - Quiz Explanation
Defining your own Java exceptions
The correct answers are indicated below, along with text that explains the correct answers.
1.
You can create a new Exception called BigException by writing
Please select the best answer.
A.
class BigException { }
B.
class BigException extends Exception implements Throwable { }
C.
class BigException extends Exception { }
The correct answer is C.
You can create a new exception simply by subclassing the class Exception. If you do not provide any new behavior for your Exception subclass, you can still have a very useful class for identifying types of exceptions in a catch block.
2.
Imagine you are catching an exception called IOException and have assigned this exception to the variable
e
.
One way to display information about the exception in your catch block is to write this line of code
Please select the best answer.
A.
e.getMessage();
B.
System.out.println(e.getMessage());
C.
System.err.println(e.getMessage());
The correct answer is B.
The getMessage() method retrieves the String describing this exception, if there is one. Then, you can display it by writing it to the standard output.
3.
You should never create and throw exceptions based on type
Please select the best answer.
A.
RuntimeException
B.
IOException
C.
Exception
The correct answer is A.
RuntimeException is an unchecked exception.
These types of exceptions should be left to Java to throw. You should only throw checked exceptions.
Java Language Reference