Error Handling Flashcards
What are the 2 main subclasses of the root class “Throwable”?
Error & Exception
- Error (Severe system errors), e.g. ThreadDeath
- Exception: e.g. NullPointerException
Do RuntimeExceptions need to be handled?
No, because RuntimeExceptions are unchecked
For which exceptions is recovery more likely? Checked or unchecked exceptions?
For checked exceptions.
Where do we try to resolve the error, in the try statement or the catch block?
In the catch block we declare how to resolve the error condition
When will the catch block be executed?
When the try block throws an error.
When we declare multiple catch blocks, which exceptions should come first: specific ones or generic exceptions?
Specific exceptions should come first, because the first matching catch-block will be executed (the rest is ignored).
Which statement is true?
a) the finally clause is only executed when the catch clause fails
b) the finally clause is always executed
c) the finally clause is only executed when the the try statement fails
Correct: b)
The finally clause is executed in any case.
Typical use: closing open files or a network connection
When defining your own checked exception. Which class do you have to extend?
a) Exception
b) Runtime Exception
Correct: a) The superclass of checked exceptions is Exception
What’s the goal of “defensive programming”?
To anticipate errors and like this reduce the number of possible bugs.
Which errors are more prone to lead to program failure (no recovery)? Multiple answers can be true.
- RuntimeExceptions
- Checked exceptions
- Unchecked exceptions
- errors of the class java.lang.Error
java.lang.Error
Unchecked exceptions
RuntimeExceptions (also unchecked)
all are unlikely to recover and in most cases will lead to program failure
What are the 4 steps of error handling?
- Error checking
- Error reporting
- Error handling
- Error recovery
What or who is the caller?
It’s a method calling another method. So basically the code itself.
Example:
public static void main(String[] args) {
System.out.println(“hello, world”);
}
-> Here the main() method is the caller
What are the 2 ways of error reporting (telling the caller, that something went wrong)?
- Have a return value which is not void.
2. Throw an exception