1/4
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
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
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
prints hello world and no errors occurred if there are no problems in the code
prints an error occurred if their is a problem with the code
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
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
you can’t divide by 0 and an error occurred if their is a problem with the code
no errors occurred if their are no problems with code
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
statement that checks if a condition is true and raises and assertion exception if the condition is false
assert
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