Free Online Flashcard Deck

8 Exceptions and Error Handling Free Online FlashCards

Study 8 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 does a syntax error differ from an exception?

Back

A syntax error prevents Python from parsing a program; an exception occurs while syntactically valid code is executing.

02
Front

What happens when an exception occurs in a try block?

Back

Python runs the try block, skips the rest if an exception occurs, and searches for a matching except clause. If none matches, the exception propagates outward.

03
Front

Why catch specific exception types?

Back

Use specific handlers such as FileNotFoundError or PermissionError so each expected failure receives an appropriate response.

04
Front

When does an else block run in exception handling?

Back

The else block runs only when the try block completes without raising an exception.

05
Front

What is the purpose of finally?

Back

A finally block runs whether or not an exception occurs, making it suitable for cleanup such as closing resources or releasing locks.

06
Front

Why prefer with for opening files?

Back

The with statement automatically closes a file, including when an exception occurs, so it is usually preferable for file resources.

07
Front

When should a function use raise?

Back

Use raise when a function detects an invalid condition and its caller should decide how to respond.

08
Front

What does a bare raise do in an except block?

Back

A bare raise sends the current exception onward while preserving its traceback, allowing local logging without claiming the failure was handled.

09
Front

What is the purpose of exception chaining?

Back

raise ... from ... preserves the original low-level exception as the explicit cause of a higher-level exception.

10
Front

What should a custom exception normally inherit from?

Back

A user-defined exception normally inherits from Exception, allowing callers to handle a meaningful application-specific error.

11
Front

Where should input validation occur?

Back

Validate data at program boundaries, such as when input enters the program, so invalid values are rejected early and clearly.

12
Front

When should code catch an exception?

Back

Catch an exception only when the function can take useful action, such as retrying, requesting new input, supplying a default, or translating the error.