3.2 error and exception handling

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/4

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

5 Terms

1
New cards
try:
print(5/0)
except:
print("you can't divide by 0")

this function uses an error exception that tries printing out a calculation that divides 5 by 0 which is impossible and uses the except function replace the error message with a print statement you can’t divide by 0

2
New cards
try:
print('hello world')
except:
print('an error occured')
else:
print('no errors occured')
finally:
print('this line runs not matter what')

this error exception

  1. prints hello world and no errors occurred if there are no problems in the code

  2. prints an error occurred if their is a problem with the code

  3. prints this line runs no matter what regardless of their being an error

    since their are no problems with this code, it prints out hello world, no errors occurred, and this line runs no matter what

3
New cards
try:
print(5/0)
except:
print("you can't divide by 0")
print('an error occured')
else:
print('no errors occured')
finally:
print('this line runs no matter what')

this error exception prints

  1. you can’t divide by 0 and an error occurred if their is a problem with the code

  2. no errors occurred if their are no problems with code

  3. this line runs not matter what regardless if their is or isn’t a problem with the code

    since you can’t divide by 0 this code prints you can’t divide by 0, an error occurred, and this line runs no matter what

4
New cards

statement that checks if a condition is true and raises and assertion exception if the condition is false

assert

5
New cards
x = 5
assert x > 10, 'x should be less than 10'

this code raises an error exception if condition is not met which in this case it’s not since the value of x is less than 10