Answer to Question #256339 in Python for Raymond Ekwubiri

Question #256339
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
Expert's answer
2021-10-25T16:55:35-0400

Catching exceptions can help with errors in that whenever a runtime error occurs, it creates an exception object. This program stops running and python prints out the traceback, which ends with a message describing the exception that occurred.

#example one
try:
    N = 42
    N = N+ ' 4 '
except TypeError:
     print('Wrong type')


#example two
try:
    m = []
    print(m[1])
except IndexError:
    print("Index error")


#Example three dividision by zero
try:
    y=int(input("Enter an interger: "))
    x = y/0
    print(x)
except ZeroDivisionError:
    print(Zero)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS