Free Online Flashcard Deck

7 Exceptions and Error Handling Free Online FlashCards

Study 7 Exceptions and Error Handling with 12 free online flashcards. Review key terms, definitions, and concepts with this interactive flashcard deck.

12 cards
01
Front

How do syntax errors differ from exceptions?

Back

A syntax error prevents Python from parsing code, whereas an exception occurs while syntactically valid code is running.

02
Front

How should a Python traceback be read?

Back

Read a traceback from the bottom upward: its final line gives the exception type and message, while preceding lines show the call path.

03
Front

When does Python raise `ValueError`?

Back

`ValueError` means the value has an acceptable general type but invalid content, as with `int("abc")`.

04
Front

When does Python raise `TypeError`?

Back

`TypeError` means an operation received an inappropriate type, such as adding the string `"total"` to the integer `3`.

05
Front

What is the control flow of `try` and `except`?

Back

`try` contains the operation that may fail; if a matching exception occurs, the remaining `try` statements are skipped and the corresponding `except` handler runs.

06
Front

What should exception handlers generally catch?

Back

Catch specific exceptions whenever possible, so expected failures are handled without hiding unrelated programming errors.

07
Front

When does a `try` statement’s `else` clause run?

Back

An `else` clause runs only when the `try` block succeeds without raising an exception.

08
Front

What is the purpose of `finally`?

Back

A `finally` clause runs whether or not an exception occurs, making it suitable for cleanup such as releasing resources or restoring state.

09
Front

Why use `with` for resource management?

Back

Prefer `with` for files and other context managers because it performs normal cleanup automatically, including when an exception occurs inside the block.

10
Front

When should code use `raise`?

Back

Use `raise` when a function cannot fulfill its contract or an input violates a required rule; for example, reject a percentage outside the range 0 through 100.

11
Front

How can a handler re-raise the current exception?

Back

Inside an `except` block, a bare `raise` re-raises the currently handled exception while preserving its traceback.

12
Front

What does `from error` provide in exception chaining?

Back

Exception chaining with `from error` translates a low-level failure into a clearer application-level exception while recording the original cause.