1 Upvote

How to Use Try Except Else Finally Statements for Error Handling in Python

try:
    # try to run process_data function
    process_data()

except Exception as e:
    # if any error occurs, print details of the exception
    print(e)

else:
    # if no error occurs print this message
    print('Ran Successfully')

finally:
    # Regardless of whether there is an error or not print this message
    print('Finished')

"""Other common types of exceptions:
ZeroDivisionError
ModuleNotFoundError
IndexError
KeyError
MemoryError
NameError
FileNotFoundError
PermissionError
TypeError
ValueError
Warning
"""

By analyseup - Last Updated June 11, 2022, 4:15 p.m.

Did you find this snippet useful?

Sign up to bookmark this in your snippet library

COMMENTS
RELATED SNIPPETS
Python For Else Loop
Python
General Python

Loop

2
Top Contributors
103
100