Describe how catching exceptions can help with file errors. Write three Python examples that actually generate file errors on your computer and catch the errors with try: except: blocks. Include the code and output for each example in your post.
Describe how you might deal with each error if you were writing a large production program. These descriptions should be general ideas in English, not actual Python code.
1)
Code:
try:
a = 1/0
print(a)
except ZeroDivisionError:
print("Zero!")
Output:
Zero!
2)
Code:
try:
a = []
print(a[1])
except IndexError:
print("Index error")
Output:
Index error
3)
Code:
try:
A = 42
A = A + ' 4 '
except TypeError:
print('Wrong type')
Output:
Wrong type
Another opportunity to deal with this errors is better development and design process. Because of algorithm work good, but there are still error, it's problem with input. So, all input data should be filtered, to prevent wrong input. It means filtering by type, size, value.
Comments
Leave a comment