10.3 Handling Exceptions Flashcards

1
Q

📝️ What is the purpose of a try-catch statement?

A

-> To separate the code that might throw an exception from the logic to handle that exception.

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

📝️ What is the flow of the code inside a try-catch statement?

A
  • > If any of the statements throws an exception the try block stops running and execution flow goes to the catch statement.
  • > If none of the statements in the try block throws an exception, the catch block is not executed
  • > If a finally-block is present, it always will be executed
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

📝️ What about Curly-braces when handling exceptions via try-catch?

A

🤯️⚠️📣️ Curly-braces are mandatory for either try-block, catch-block and finally-block

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

📝️ How to understand given exception classes?

A
  • > First, you must be able to recognize if the exception is a checked or an unchecked exception.
  • > Second, you need to determine whether any of the exceptions are subclasses of the others.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

🤯️⚠️📣️ When does Unreachable catch blocks generates a compiler error?

A

-> This happens when a superclass catch block appears before a subclass catch block.

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

Subclasses you need to recall…

A
  • > NumberFormatException is subclass of IllegalArgumentException (Unchecked)
  • > FileNotFoundException is subclass of IOException (Checked)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

🤯️⚠️📣️ What about the scope of a defined exception variable within the catch statement?

A

-> An exception variable is only within the scope for the catch block were it’s defined

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

📝️ What is a multi-catch statement?

A

A multi-catch block allows multiple exception types to be caught by the same catch block.

🤯️⚠️📣️ Notice that a multi-catch block defines a single identifier for all exception types.

✅ catch(Exception1 | Exception2 | Exception3 e)

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

📝️ What are the multi-catch statement ⚠️PITFALLS⚠️?

A

🤯️⚠️📣️ Alternatives in a multi-catch statement cannot be related by subclassing

🤯️⚠️📣️ Notice that a multi-catch block defines a single identifier for all exception types.

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