Module 11 - Exceptions Flashcards

1
Q

What is an exception?

A

Error that occurs while a program is running

Usually causes program to abruptly halt

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

What is a traceback?

A

Error message that gives information regarding line numbers that caused the exception
Tells you type of exception and brief description of the error

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

What are some examples of exceptions that cannot be avoided by careful coding?

A

Trying to convert non-numeric string to an integer

Trying to open for reading a file that doesn’t exist

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

What is an exception handler?

A

Code that responds when exceptions are raised and prevents program from crashing

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

What is the exception handler statement in Python?

A

try/except statement

try:
___statements
except exceptionName:
___statements

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

How does the try/except statement work?

A
You can enter whatever code you want in the 'try' suite
The handler (i.e., the except block) will execute if the 'try' suite has an exception
If no exception is raised, handler is skipped
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Which three steps are KEY in writing professional grade exception handling code?

A
  1. Account for unavoidable exceptions (adding the except statement)
  2. Inform the end user that an error has occurred by displaying a USER FRIENDLY message
  3. Take further action by retrying or saving existing work when possible and if applicable
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Often code in try suite can throw more than one type of exception. How do you handle this?

A

Need to write except clause for each type of exception that needs to be handled

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

What happens when an except clause does not list a specific exception?

A

It will handle ANY exception raised in the ‘try suite’

Should always be last in the series of except clauses!

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

What is an exception object?

A

Object created in memory when an exception is thrown

It contains a default error message pertaining to the exception

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

What can you do with an exception object?

A

Can assign the exception object to a variable in an except clause
Example: except ValueError as err:
Can pass exception object variable to print function to display the default error message

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

What is the ‘else’ clause?

A

Optional block of code AFTER the try and except blocks that runs IF there are no exceptions
(if exceptions, else suite is skipped)

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

What is the ‘finally’ clause?

A

Optional block of code AFTER the try and except blocks - execute whether an exception occurs or not.
Purpose is to perform cleanup before exiting.

finally:
___statements

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

What are the two ways for an exception to go unhandled?

A
  1. No except clause specifying exception of the right type

2. Exception raised outside a try suite

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

What happens when an exception goes unhandled?

A

Exception will cause the program to halt

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

Handling an exception with a try statement is called ____ing an exception.

A

Catching
(To prevent an exception from terminating a program using the try and except statements)

It gives you a chance to fix the problem or try again, or at least end the program gracefully