Error handling is the important concept of this class.
This class will cover:
- error handling
- excptions
- using
try
andexcept
- best practices using
with
- clean up our functions with
finally
- open, closing and writing to files
What can go wrong will go wrong
Assume your code will break, and what you want to do is handle the errors, gracefully.
When you handle your errors, your code will continue to run.
You never want to handle for ALL EXCEPTION because it can create an unstopable code.
You must specify what exception you want to handle:
try:
file = open('order.txt')
except FileNotFoundError
print('THERE HAS BEEN AN ERROR! PANIC NOW!')
You can capture your messages using as
:
try:
file = open('order.txt')
except FileNotFoundError as error_message:
# print('THERE HAS BEEN AN ERROR! PANIC NOW!')
It's when the code actually breaks / stops. Unless handled. / stops. Unless handled.