10.3 Handling Exceptions Flashcards
📝️ What is the purpose of a try-catch statement?
-> To separate the code that might throw an exception from the logic to handle that exception.
📝️ What is the flow of the code inside a try-catch statement?
- > 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
📝️ What about Curly-braces when handling exceptions via try-catch?
🤯️⚠️📣️ Curly-braces are mandatory for either try-block, catch-block and finally-block
📝️ How to understand given exception classes?
- > 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.
🤯️⚠️📣️ When does Unreachable catch blocks generates a compiler error?
-> This happens when a superclass catch block appears before a subclass catch block.
Subclasses you need to recall…
- > NumberFormatException is subclass of IllegalArgumentException (Unchecked)
- > FileNotFoundException is subclass of IOException (Checked)
🤯️⚠️📣️ What about the scope of a defined exception variable within the catch statement?
-> An exception variable is only within the scope for the catch block were it’s defined
📝️ What is a multi-catch statement?
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)
📝️ What are the multi-catch statement ⚠️PITFALLS⚠️?
🤯️⚠️📣️ 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.