Exceptions Flashcards

(48 cards)

1
Q

What are the two main types of errors in Python?

A

Syntax errors and exceptions.

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

What is a syntax error in Python?

A

A parsing error indicating that code does not conform to Python’s grammatical rules.

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

How does Python indicate the location of a syntax error?

A

Python outputs the offending line and character position, along with the file name and line number if applicable.

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

What is an exception in Python?

A

An error detected during execution that does not necessarily stop the program and can be handled.

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

What happens if an exception is not handled in Python?

A

The program displays an error message and typically halts execution.

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

What information does the last line of a Python exception message provide?

A

It specifies the type of exception that occurred, such as ZeroDivisionError, NameError, or TypeError.

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

Are standard exception names special keywords in Python?

A

No, they are built-in identifiers but not reserved keywords.

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

What additional details does a Python exception message provide beyond the type?

A

It contains specific details based on the exception type and the cause.

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

How does Python display the context of an exception?

A

Using a stack traceback that shows the sequence of code that led to the error.

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

What section of Python documentation lists built-in exceptions and their meanings?

A

The Built-in Exceptions section.

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

How can a Python program handle specific exceptions?

A

By using a try statement with except clauses for selected exceptions.

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

What operations occur within a try statement in Python?

A

The try block runs; if no exception occurs, except is skipped, else it is executed if present.

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

What happens if an exception occurs within the try block and matches an except clause?

A

The matching handler runs, and execution resumes after the try/except block.

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

What does Python do if an exception is raised in the try block but does not match any except clause?

A

It passes the exception to outer try statements or stops execution with an error if unhandled.

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

Can a try statement in Python have multiple except clauses?

A

Yes, to handle different exception types.

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

How does Python determine which except handler to execute when multiple match?

A

Only the first matching handler is executed.

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

What exceptions does a class named in an except clause match?

A

Exceptions that are instances of that class or its subclasses, but not base classes.

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

How can a single except clause handle multiple exceptions in Python?

A

By specifying a parenthesized tuple of exception types.

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

Can exception handlers capture exceptions that arise in called functions within the try block?

A

Yes, handlers process exceptions from functions called from the try block.

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

How can a variable be assigned to a caught exception in except?

A

By specifying a variable after the exception type in the except statement.

21
Q

What attribute usually holds the arguments for an exception instance?

A

The .args attribute.

22
Q

How does the __str__() method of an exception relate to the error message?

A

Its output is shown as the final part of an unhandled exception message.

23
Q

What class is the base for all exceptions in Python?

A

BaseException.

24
Q

What is the base class for non-fatal exceptions

A

and which exceptions are usually not handled?

25
Is it advisable to use Exception as a catch-all in except statements?
It works but being more specific is considered better practice.
26
What is the typical pattern for handling and propagating exceptions?
Log or print the exception, then re-raise it to let callers handle it.
27
What is the purpose of the else clause in a try…except statement?
To execute code if no exception was raised, reducing the risk of catching unrelated errors.
28
Do exception handlers in a try block handle exceptions from functions called within the block?
Yes, exceptions from such functions are also handled.
29
How can you force an exception to occur in Python?
By using the raise statement with an exception instance or exception class.
30
How can you re-raise an exception without handling it?
By using raise without arguments.
31
What happens when an exception occurs inside an except block?
The original exception is attached as context to the new exception.
32
How does the ‘raise ... from ...’ syntax help with exceptions?
It explicitly sets the cause of a raised exception, clarifying exception chaining.
33
How can you disable automatic exception chaining in Python?
By using from None with the raise statement.
34
How are user-defined exceptions created in Python?
By defining a new class that inherits from Exception, directly or indirectly.
35
What is the common naming convention for exception classes?
Exception class names usually end with 'Error'.
36
How complex can a custom exception class be in Python?
It can be complex, but is usually simple with attributes for relevant information.
37
Do Python standard modules define their own exceptions?
Yes, many modules define specific exception classes.
38
What is the function of the finally clause in a try block?
To ensure clean-up code runs regardless of exceptions.
39
When does the finally clause execute?
Always, upon completion of the try block, whether an exception occurred or not.
40
What happens if both try and finally have return statements?
The value from the finally clause is returned.
41
Why is the finally clause useful in real applications?
It reliably releases resources like files or network connections.
42
What is the with statement used for in Python resource management?
It ensures predefined clean-up actions are performed, such as closing files.
43
What does ExceptionGroup allow in Python 3.11+?
It wraps multiple unrelated exceptions to be raised and handled together.
44
How can you handle specific exceptions within an ExceptionGroup?
By using except* to handle selected exception types in the group.
45
What must be true about exceptions nested in an ExceptionGroup?
They must be exception instances, not types.
46
How can you add contextual information to an exception after catching it?
By calling add_note(note) on the exception object.
47
What is displayed in a traceback for an exception with notes?
All added notes are shown in traceback after the exception, in order added.
48
How can contextual notes be useful with ExceptionGroup?
They help identify when and where each wrapped error occurred.