Error Handling Flashcards

1
Q

What are the 2 main subclasses of the root class “Throwable”?

A

Error & Exception

  • Error (Severe system errors), e.g. ThreadDeath
  • Exception: e.g. NullPointerException
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Do RuntimeExceptions need to be handled?

A

No, because RuntimeExceptions are unchecked

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

For which exceptions is recovery more likely? Checked or unchecked exceptions?

A

For checked exceptions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Where do we try to resolve the error, in the try statement or the catch block?

A

In the catch block we declare how to resolve the error condition

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

When will the catch block be executed?

A

When the try block throws an error.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

When we declare multiple catch blocks, which exceptions should come first: specific ones or generic exceptions?

A

Specific exceptions should come first, because the first matching catch-block will be executed (the rest is ignored).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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

A

Correct: b)
The finally clause is executed in any case.

Typical use: closing open files or a network connection

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

When defining your own checked exception. Which class do you have to extend?

a) Exception
b) Runtime Exception

A
Correct: a) 
The superclass of checked exceptions is Exception
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What’s the goal of “defensive programming”?

A

To anticipate errors and like this reduce the number of possible bugs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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
A

java.lang.Error
Unchecked exceptions
RuntimeExceptions (also unchecked)

all are unlikely to recover and in most cases will lead to program failure

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the 4 steps of error handling?

A
  1. Error checking
  2. Error reporting
  3. Error handling
  4. Error recovery
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What or who is the caller?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are the 2 ways of error reporting (telling the caller, that something went wrong)?

A
  1. Have a return value which is not void.

2. Throw an exception

How well did you know this?
1
Not at all
2
3
4
5
Perfectly