Python Web-L06 (try...except..)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
a = 10
b = 0
try:
result = a/b
except ZeroDivisionError:
print("data error")
except TypeError:
print("check data type")
finally: # finally statement -> protect data files
print("this line will run not matter what !")
a = 10
b = 'a'
try:
result = a/b
except ZeroDivisionError:
print("data error")
except TypeError:
print("check data type")
finally: # finally statement -> protect data files
print("this line will run not matter what !")
# case 3
a = -2
b = 2
while a < 3:
print('------------')
a+=1
b-=1
try:
res = a / b
except ZeroDivisionError:
print('{0}, {1} - division by 0'.format(a,b))
res = 0
continue
finally:
print('{0}, {1} - always executes'.format(a,b))
# case 4
a = 0
b = 2
while a < 3:
print('------------')
a+=1
b-=1
try:
res = a / b
except ZeroDivisionError:
print('{0}, {1} - division by 0'.format(a,b))
res = 0
break
finally:
print('{0}, {1} - always executes'.format(a,b))
# case 5
a = 3
b = 2
while a < 3:
print('------------')
a+=1
b-=1
try:
res = a / b
except ZeroDivisionError:
print('{0}, {1} - division by 0'.format(a,b))
res = 0
continue
finally:
print('{0}, {1} - always executes'.format(a,b))
else:
print('\n\nno errors were encountered!')
# case 6
a = 0
b = 5
while a < 3:
print('-----case 6-------')
a+=1
b-=1
try:
res = a / b
except ZeroDivisionError:
print('{0}, {1} - division by 0'.format(a,b))
res = 0
continue
finally:
print('{0}, {1} - always executes'.format(a,b))
print('{0}, {1} - main loop'.format(a,b))
else:
print('\n\nno errors were encountered!')
Python
INFO