6.8 Try, Except Flashcards

1
Q

What does “Try, Except” statement does?

A

This statement controls how the program proceeds when the error occurs

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

What is the syntax for “Try, Except” statement?

A

try:
do something
except:
do something else when error occurs

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

Write a script using the “Try, Except” statement

A
try:
   answer = 12/0
   print (answer)
except:
   print("An error occurred")

It will print - An error occurred as you cannot divide a number by Zero

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

What would you do if you want to display more specific error messages to your users, depending on the error?

A

One can specify the error type after the except keyword

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

Write a script and result using the multiple levels of Except

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

When is ValueError raised?

A

When a function receives an argument that has the right type, but an inappropriate value like when asking for a number, a letter is entered.

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

When is ZeroDivisionError raised?

A

When the program tries to divide by Zero

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

When is IOError raised?

A

It is raised when an I/O operation fails for an I/O-related reason, e.g.,”file not found”.

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

When is importError raised?

A

It is raised when an import statement fails to find the module definition.

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

When is IndexError raised?

A

When a sequence(e.g. string, list, tuple) index is out of range

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

When is KeyError raised?

A

When a dictionary key isn’t found.

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

When is NameError raised?

A

When a local or global name is not found

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

TypeError

A

When an operation or function is applied to an object of inqppropriate type

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

What keyword is used for the pre-defined error messages?

A

“as”

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

Give an example to display the default ValueError message.

A

except ValueError as e:

print(e)

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