Lecture 13 - Exceptions, Assertions Flashcards

(42 cards)

1
Q

What is an Exception?

A

An error that occurs during the runtime of a program, disrupting the normal flow of execution.

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

What does Raising (or Throwing) an Exception mean?

A

The act of generating a new exception instance when an error condition is detected.

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

What is Handling an Exception?

A

The process of intercepting and responding to a raised exception, preventing the program from crashing.

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

What is a Try Block?

A

A block of code that is monitored for potential exceptions.

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

What is an Except Clause?

A

A block of code that is executed if a specific type of exception occurs within the associated try block. Multiple except clauses can handle different exception types.

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

What is the purpose of an Else Clause (Optional)?

A

A block of code that is executed if and only if no exception was raised within the preceding try block.

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

What is a Finally Clause (Optional)?

A

A block of code that is always executed after the try block finishes, regardless of whether an exception was raised or handled. Useful for cleanup operations.

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

What is an Exception Object?

A

An instance of a specific exception class, containing information about the error. It can be accessed in the except clause using the as keyword.

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

What is the Exception Hierarchy?

A

Exceptions in Python are organised into a class hierarchy. Catching a parent class exception (e.g., Exception) will also catch its subclasses (e.g., ZeroDivisionError).

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

What are Custom Exceptions?

A

User-defined exception classes created by subclassing the built-in Exception class or its descendants.

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

What are Assertions?

A

Statements used to check if a condition is true. If the condition is false, an AssertionError is raised, halting the program’s execution. Primarily used for debugging and enforcing assumptions during development.

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

What is __debug__?

A

A built-in Boolean constant that is True in normal mode and False when Python is run with optimization flags (like -O). Assertions are skipped when __debug__ is False.

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

What is Defensive Programming?

A

Writing code with the intention of handling potential errors and unexpected conditions gracefully.

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

Explain the purpose of exception handling in Python. Why is it important for writing robust programs?

A

Exception handling allows a program to gracefully recover from runtime errors, preventing it from crashing. This is crucial for robustness as it enables the program to manage unexpected situations, such as invalid user input or file access issues, and continue execution or terminate in a controlled manner.

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

Describe the structure of a try-except block. What is the role of the try keyword and the except keyword?

A

A try-except block is used to handle potential exceptions. The code that might raise an exception is placed within the try block. If an exception of a specified type occurs within the try block, the program’s execution immediately jumps to the corresponding except clause, where code to handle that specific exception is located.

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

What is the significance of specifying the type of exception in an except clause? How does Python determine which except block to execute?

A

Specifying the exception type in an except clause allows you to handle different kinds of errors in specific ways. Python checks the type of the raised exception against the types listed in the except clauses. The first except clause whose exception type matches the raised exception (or is a superclass of it) will be executed.

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

Explain the functionality of the optional else clause in a try-except block. Under what circumstances is the code within the else clause executed?

A

The optional else clause is executed only if the code within the try block completes without raising any exceptions. It is used for code that depends on the successful execution of the try block and should not be run if an error occurred.

18
Q

Describe the purpose of the finally clause in exception handling. When is the code in a finally block guaranteed to run?

A

The finally clause is used for code that must be executed regardless of whether an exception was raised in the try block or not, and regardless of whether the exception was handled. It is commonly used for cleanup operations such as closing files or releasing resources.

19
Q

How can you access the exception object that was caught by an except clause? Provide a brief example.

A

You can access the exception object using the as keyword in the except clause. For example: try: … except ValueError as e: print(f”A ValueError occurred: {e}”). Here, e is a variable that will hold the instance of the ValueError exception.

20
Q

What happens to the normal flow of execution within a try block when an exception is raised? What happens to the code following the try-except block?

A

When an exception is raised within a try block, the remaining code in the try block is skipped, and execution immediately transfers to the appropriate except clause. After the try-except (and optional else and finally) block has finished executing, the program continues with the statement following the entire construct.

21
Q

Explain the primary use case for assert statements in Python. How do they differ from standard exception handling with try-except?

A

assert statements are primarily used as a debugging aid to check if certain conditions that should always be true during development are indeed true. If an assertion fails, it indicates a bug in the code. They differ from try-except in that they are intended to halt execution immediately when an unexpected condition arises, rather than gracefully handling anticipated errors.

22
Q

What is the AssertionError exception? Under what condition is it raised?

A

AssertionError is the exception raised when an assert statement evaluates the given condition as False. It signals that an assumption made by the programmer about the state of the program has been violated, typically indicating a bug.

23
Q

How can you disable assert statements in Python? What is the purpose of disabling them in production code?

A

assert statements can be disabled by running Python in optimized mode using the -O or -OO command-line flags, or by setting the PYTHONOPTIMIZE environment variable. Disabling assertions in production can improve performance by skipping these checks, as it is assumed that the code has been thoroughly debugged and tested.

24
Q

What is an Arithmetic Exception?

A

A category of exceptions that occur during arithmetic operations, such as division by zero

Example: ZeroDivisionError

25
What does an Assertion do?
A statement that tests whether a condition is true. If false, it raises an AssertionError.
26
What is an AssertionError?
The exception raised when an assert statement fails (the condition is false).
27
What are Built-in Exceptions?
Exceptions that are predefined in the Python language, covering common error conditions ## Footnote Examples: TypeError, ValueError, IndexError
28
What is a Catch Block?
Another term for the except clause, which is responsible for handling a specific type of exception.
29
What is a Custom Exception?
An exception defined by the programmer, usually by creating a new class that inherits from the Exception base class.
30
What is Debugging?
The process of identifying and removing errors (bugs) from computer programs.
31
What is an Error in programming?
A general term for a problem that can occur in a program. In Python, errors can often lead to exceptions.
32
What is an Exception Handler?
The code within an except block that is executed when a specific exception is raised.
33
What does an IndexError indicate?
An exception raised when trying to access an index that is out of range for a sequence (like a list or string).
34
What is a NameError?
An exception raised when a local or global name is not found.
35
What does the keyword 'Raise' do?
The keyword used to explicitly generate (throw) an exception.
36
What is Rethrowing?
Catching an exception in an except block and then immediately raising it again.
37
What is a Runtime Error?
An error that occurs during the execution of a program. Exceptions are a type of runtime error.
38
What does a Signal (Exception Context) indicate?
A specific type of exception, indicating a particular error condition.
39
What is a SyntaxError?
An exception raised when the Python interpreter encounters code that violates the language's syntax rules.
40
What is a TypeError?
An exception raised when an operation or function is applied to an object of an inappropriate type.
41
What is a ValueError?
An exception raised when an operation or function receives an argument of the correct type but an inappropriate value.
42
What does a ZeroDivisionError indicate?
An arithmetic exception raised when attempting to divide a number by zero.