v Try, Except, and Finally - Python

Try, Except, and Finally

Create data

# Create some data
scores = [23,453,54,235,74,234]

Try something that doesn't work

# Try to:
try:
    # Add a list of integers and a string
    scores + 'A string of characters.'
# If you get an error, set the error as 'e',
except Exception as e:
    # print the error, e
    print('Error:', e)
# Then,
finally:
    # print end program
    print('End Program')
Error: can only concatenate list (not "str") to list
End Program

Try something that works

# Try to:
try:
    # Print scores
    print('Worked!', scores)
# If you get an error, set the error as 'e',
except Exception as e:
    # print the error, e
    print('Error:', e)
# Then,
finally:
    # print end program
    print('End program')
Worked! [23, 453, 54, 235, 74, 234]
End program