Error handling

This commit is contained in:
Hizenberg469
2025-03-20 21:02:12 +05:30
parent a0795f56ab
commit 3a195b2260
3 changed files with 71 additions and 0 deletions

53
error-handling/wiki.txt Normal file
View File

@@ -0,0 +1,53 @@
-> Error handling in python:
for ex:
while True:
try:
age = int(input('what is your age? '))
print(age)
except:
print('please enter a number')
else:
print('thank you')
break
for ex:
def sum(num1, num2):
try:
return num1 + num2
except TypeError as err:
print(f'please enter a number {err}')
print(sum(1, '2'))
for ex:
def sum(num1, num2):
try:
return num1/num2
except (TypeError, ZeroDivisionError):
print(f'please enter a number {err}')
print(sum(1, '2'))
-> How to throw your own error?
for ex:
while True:
try:
age = int(input('what is your age? '))
10/age
raise ValueError('hey cut it out')
except ValueError:
print('please enter a valid number')
except ZeroDivisionError:
print('please enter age higher than 0')
break
else:
print('thank you')
break
finally:
print('ok, I\'m finally done')
print('can you hear me?')