[Python] #5 Python Exception Handling
This is a self note while taking the online course from: LinkedIn Learning: Learning Python by Joe Marini LinkedIn Learning: Python Essential Training by Bill Weinman
Exception
- try -> except ___Error -> except -> else (NO Errors)
- use sys.exc_info()[1] for details about an error
- to raise error(exception): use ‘raise’ keyword
import sys
try:
# codes that might raise exceptions
# Code below raise exception
"""
raise TypeError('raised TypeError intenionally')
"""
except Error_Name (as e):
print(f'caught {e}')
except:
# other exceptions
print(f'unkonwn error: {sys.exc_info()[1]}')
else:
# No error raised!