Week 4- Exception Handling Flashcards

(11 cards)

1
Q

What are errors that occur during the execution called?

A

Exceptions

Exceptions are primarily caused by environmental issues such as reading a non-existent file or bad user inputs.

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

What is the main purpose of the try…except statement?

A

To handle exceptions that may occur in the execution of code

The statements in the try clause execute first, and if no exceptions occur, the except clause is skipped.

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

What happens if an exception occurs in the try clause?

A

The rest of the try clause is skipped, and the except clause is executed

This allows for graceful handling of errors.

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

How can multiple exceptions be handled in Python?

A

By specifying multiple except clauses or grouping exceptions into one clause

This allows for a unified response to similar types of exceptions.

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

What is the role of the finally clause in exception handling?

A

It always executes whether an exception occurs or not

The finally clause executes after the try and any except clauses.

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

What does the else clause do in a try…except…else statement?

A

It executes if no exception occurs in the try clause

The else clause runs after the try clause and before the finally clause if included.

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

What are custom exceptions in Python?

A

Exceptions defined by subclassing Python’s built-in Exception class

Custom exceptions allow for domain-specific error handling.

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

What is exception chaining in Python?

A

Using raise … from … to propagate an exception while preserving the original context

This helps in debugging by maintaining the context of the original error.

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

What feature was introduced in Python 3.11 for handling multiple exceptions?

A

ExceptionGroups

This allows for simultaneous handling of multiple exceptions.

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

What is the purpose of context managers in exception handling?

A

To manage resources and handle exceptions using the with statement

The __exit__ method of a context manager can handle exceptions that occur within the context.

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

What does re-raising an exception involve?

A

Catching an exception, performing actions, and then using raise without arguments to re-raise it

This allows for additional handling or logging before the exception propagates.

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